Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include listeners reference in testng-failed.xml #2882

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-2879: Test listeners specified in parent testng.xml file are not included in testng-failed.xml file (Krishnan Mahadevan)
Fixed: GITHUB-2866: TestNG.xml doesn't honour Parallel value of a clone (Krishnan Mahadevan)
Fixed: GITHUB-2875: JUnitReportReporter should capture the test case output at the test case level
Fixed: GITHUB-2771: After upgrading to TestNG 7.5.0, setting ITestResult.status to FAILURE doesn't fail the test anymore (Julien Herr & Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ protected void generateFailureSuite(XmlSuite xmlSuite, ISuite suite, String outp
}

if (null != failedSuite.getTests() && failedSuite.getTests().size() > 0) {
if (xmlSuite.getParentSuite() != null
&& !xmlSuite.getParentSuite().getLocalListeners().isEmpty()) {
List<String> merged =
Lists.merge(failedSuite.getListeners(), xmlSuite.getParentSuite().getListeners());
failedSuite.setListeners(merged);
}
Utils.writeUtf8File(outputDir, TESTNG_FAILED_XML, failedSuite.toXml());
Utils.writeUtf8File(suite.getOutputDirectory(), TESTNG_FAILED_XML, failedSuite.toXml());
}
Expand Down
50 changes: 50 additions & 0 deletions testng-core/src/test/java/test/reports/FailedReporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.testng.Assert;
Expand All @@ -32,9 +33,58 @@
import test.reports.issue2611.TestClassWithBeforeSuiteSample;
import test.reports.issue2611.TestClassWithBeforeTestSample;
import test.reports.issue2611.TestClassWithJustTestMethodsSample;
import test.reports.issue2879.AnotherPrintingListener;
import test.reports.issue2879.PrintingListener;
import test.reports.issue2879.TestClassSample;

public class FailedReporterTest extends SimpleBaseTest {

@Test(description = "GITHUB-2879")
public void ensureParentListenersArePresentInFailedChildSuites() throws IOException {
XmlSuite xmlParentSuite = createXmlSuite("parent_suite");
xmlParentSuite.setListeners(Collections.singletonList(PrintingListener.class.getName()));
XmlSuite xmlSuite = createXmlSuite("2879_suite");
xmlSuite.setParentSuite(xmlParentSuite);
XmlTest xmlChildTest = createXmlTest(xmlSuite, "2879_test");
xmlChildTest.setClasses(Collections.singletonList(new XmlClass(TestClassSample.class)));
xmlParentSuite.getChildSuites().add(xmlSuite);
TestNG tng = create(xmlParentSuite);
Path temp = Files.createTempDirectory("tmp");
tng.setOutputDirectory(temp.toAbsolutePath().toString());
tng.addListener(new FailedReporter());
tng.run();

Collection<XmlSuite> failedSuites =
new Parser(temp.resolve(FailedReporter.TESTNG_FAILED_XML).toAbsolutePath().toString())
.parse();
XmlSuite failedSuite = failedSuites.iterator().next();
assertThat(failedSuite.getListeners()).containsExactly(PrintingListener.class.getName());
}

@Test(description = "GITHUB-2879")
public void ensureParentListenersAreAppendedInFailedChildSuites() throws IOException {
XmlSuite xmlParentSuite = createXmlSuite("parent_suite");
xmlParentSuite.setListeners(Collections.singletonList(PrintingListener.class.getName()));
XmlSuite xmlSuite = createXmlSuite("2879_suite");
xmlSuite.setParentSuite(xmlParentSuite);
xmlSuite.setListeners(Collections.singletonList(AnotherPrintingListener.class.getName()));
XmlTest xmlChildTest = createXmlTest(xmlSuite, "2879_test");
xmlChildTest.setClasses(Collections.singletonList(new XmlClass(TestClassSample.class)));
xmlParentSuite.getChildSuites().add(xmlSuite);
TestNG tng = create(xmlParentSuite);
Path temp = Files.createTempDirectory("tmp");
tng.setOutputDirectory(temp.toAbsolutePath().toString());
tng.addListener(new FailedReporter());
tng.run();

Collection<XmlSuite> failedSuites =
new Parser(temp.resolve(FailedReporter.TESTNG_FAILED_XML).toAbsolutePath().toString())
.parse();
XmlSuite failedSuite = failedSuites.iterator().next();
assertThat(failedSuite.getListeners())
.contains(PrintingListener.class.getName(), AnotherPrintingListener.class.getName());
}

@Test
public void failedFile() throws IOException {
XmlSuite xmlSuite = createXmlSuite("Suite");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test.reports.issue2879;

import org.testng.IExecutionListener;

public class AnotherPrintingListener implements IExecutionListener {

@Override
public void onExecutionStart() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.reports.issue2879;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class PrintingListener implements IInvokedMethodListener {

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test.reports.issue2879;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestClassSample {

@Test
public void passingOne() {}

@Test
public void passingTwo() {}

@Test
public void failingOne() {
Assert.fail();
}

@Test
public void failingTwo() {
Assert.fail();
}
}