Skip to content

Commit

Permalink
Copy test result attributes when unexpected failures
Browse files Browse the repository at this point in the history
Closes #3064
  • Loading branch information
krmahadevan committed Feb 12, 2024
1 parent 1774b2c commit e77f1c2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Current
7.10.0
Fixed: GITHUB-3064: TestResult lost if failure creating RetryAnalyzer (Krishnan Mahadevan)
Fixed: GITHUB-3048: ConcurrentModificationException when injecting values (Krishnan Mahadevan)
Fixed: GITHUB-3050: Race condition when creating Guice Modules (Krishnan Mahadevan)
Fixed: GITHUB-3059: Support the ability to inject custom listener factory (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ public int invoke(int invCount) {
ITestResult r =
TestResult.newEndTimeAwareTestResult(
arguments.getTestMethod(), m_testContext, cause, start);
Optional.ofNullable(Reporter.getCurrentTestResult())
.ifPresent(it -> TestResult.copyAttributes(it, r));
r.setStatus(TestResult.FAILURE);
result.add(r);
runTestResultListener(r);
Expand Down
10 changes: 10 additions & 0 deletions testng-core/src/test/java/test/listeners/ListenersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import test.listeners.issue2880.ListenerForIssue2880;
import test.listeners.issue2880.TestClassWithFailingConfigsSample;
import test.listeners.issue2880.TestClassWithPassingConfigsSample;
import test.listeners.issue3064.EvidenceListener;
import test.listeners.issue3064.SampleTestCase;

public class ListenersTest extends SimpleBaseTest {

Expand All @@ -37,6 +39,14 @@ public class ListenersTest extends SimpleBaseTest {
"test.listeners.issue2638.TestClassBSample.testMethod"
};

@Test(description = "GITHUB-3064")
public void ensureTestResultAttributesAreCarriedForward() {
TestNG testng = create(SampleTestCase.class);
testng.run();
assertThat(EvidenceListener.failureTestResult.getAttribute(EvidenceListener.ATTRIBUTE_KEY))
.isEqualTo("attributeValue");
}

@Test(description = "GITHUB-2638", dataProvider = "suiteProvider")
public void ensureDuplicateListenersAreNotWiredInAcrossSuites(
XmlSuite xmlSuite, Map<String, String[]> expected) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test.listeners.issue3064;

import org.testng.ITestListener;
import org.testng.ITestResult;

public class EvidenceListener implements ITestListener {
public static final String ATTRIBUTE_KEY = "attributeKey";
public static ITestResult failureTestResult;

@Override
public void onTestStart(ITestResult result) {
result.setAttribute(ATTRIBUTE_KEY, "attributeValue");
}

@Override
public void onTestFailure(ITestResult result) {
failureTestResult = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.listeners.issue3064;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class EvidenceRetryAnalyzer implements IRetryAnalyzer {

public EvidenceRetryAnalyzer() {
throw new RuntimeException("Failed on purpose");
}

@Override
public boolean retry(ITestResult result) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package test.listeners.issue3064;

import static org.testng.Assert.fail;

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.Reporter;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Test
@Listeners(EvidenceListener.class)
public class SampleTestCase {

@BeforeSuite(alwaysRun = true)
public void suiteSetup() {
ITestContext context = Reporter.getCurrentTestResult().getTestContext();
for (ITestNGMethod method : context.getAllTestMethods()) {
method.setRetryAnalyzerClass(EvidenceRetryAnalyzer.class);
}
}

@Test
public void testOne() {
fail();
}
}

0 comments on commit e77f1c2

Please sign in to comment.