Skip to content

Commit

Permalink
Ensure ITestResult injected to @AfterMethod is apt
Browse files Browse the repository at this point in the history
Closes #3006
  • Loading branch information
krmahadevan committed Nov 21, 2023
1 parent 94f3373 commit 1d9dba8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-3006: ITestResult injected at @AfterMethod incorrect when a configuration method failed (Krishnan Mahadevan)
Fixed: GITHUB-3003: BeforeClass|AfterClass with inheritedGroups triggers cyclic dependencies (Krishnan Mahadevan)
New: Added @Inherited to the Listeners annotation, allowing it to be used in forming meta-annotations. (Pavlo Glushchenko)
Fixed: GITHUB-2991: Suite attributes map should be thread safe (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,9 @@ private ITestResult invokeMethod(
TestResult.copyAttributes(testResult, result);
m_notifier.addSkippedTest(arguments.getTestMethod(), result);
arguments.getTestMethod().incrementCurrentInvocationCount();
testResult.setMethod(arguments.getTestMethod());
invokedMethod = new InvokedMethod(startTime, result);
invokeListenersForSkippedTestResult(result, invokedMethod);
runAfterConfigurations(arguments, suite, testResult);
runAfterConfigurations(arguments, suite, result);
runAfterGroupsConfigurations(arguments);

return result;
Expand Down Expand Up @@ -773,7 +772,7 @@ private static void cleanInterruptStatus() {
}

private void runAfterConfigurations(
TestMethodArguments arguments, XmlSuite suite, TestResult testResult) {
TestMethodArguments arguments, XmlSuite suite, ITestResult testResult) {
ITestNGMethod[] teardownConfigMethods =
TestNgMethodUtils.filterTeardownConfigurationMethods(
arguments.getTestMethod(), arguments.getAfterMethods());
Expand All @@ -795,7 +794,7 @@ private void runAfterGroupsConfigurations(TestMethodArguments arguments) {
private void runConfigMethods(
TestMethodArguments arguments,
XmlSuite suite,
TestResult testResult,
ITestResult testResult,
ITestNGMethod[] teardownConfigMethods) {
ConfigMethodArguments cfgArgs =
new ConfigMethodArguments.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.testng.Assert;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -76,6 +79,22 @@ public void ensureGroupInheritanceWorksForConfigMethods() {
assertThat(test.configuration.issue3003.TestClassSample.logs).containsAll(expected);
}

@Test(description = "GITHUB-3006")
public void ensureNativelyInjectedTestResultForAfterMethodMatchesTestMethod() {
TestNG testng = create(test.configuration.issue3006.TestClassSample.class);
testng.run();
ITestResult actual = test.configuration.issue3006.TestClassSample.iTestResult;
assertThat(actual.getStatus())
.withFailMessage("The test method status should have been SKIPPED")
.isEqualTo(ITestResult.SKIP);
List<String> skippedDueTo =
actual.getSkipCausedBy().stream()
.map(ITestNGMethod::getQualifiedName)
.collect(Collectors.toList());
assertThat(skippedDueTo)
.containsExactly("test.configuration.issue3006.TestClassSample.beforeMethod");
}

@DataProvider(name = "produceTestClasses")
public Object[][] produceTestClasses() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test.configuration.issue3006;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClassSample {

public static ITestResult iTestResult;

@BeforeMethod
public void beforeMethod() {
throw new RuntimeException("Exception to simulate configuration error");
}

@Test
public void test() {}

@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult testResult) {
iTestResult = testResult;
}
}

0 comments on commit 1d9dba8

Please sign in to comment.