Skip to content

Commit

Permalink
Ignore Config skips in xml reports
Browse files Browse the repository at this point in the history
Closes #2886

TestNG by design excludes suite level configs 
When there is inheritance involved and multiple 
Children of the same base class are included in a suite. 
But when reporting the results as xml 
TestNG is not excluding these configs from the reports.

Fixed this discrepancy.
  • Loading branch information
krmahadevan committed Mar 24, 2023
1 parent ff5843a commit 2e0c8bd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Fixed: GITHUB-2862: Allow test classes to define "configfailurepolicy" at a per
Fixed: GITHUB-2796: Option for onAfterClass to run after @AfterClass (Oliver Hughes)
Fixed: GITHUB-2857: XmlTest index is not set for test suites invoked with YAML (Sergei Baranov)
Fixed: GITHUB-2880: Before configuration and before invocation set 'SKIP' when beforeMethod is 'skip' (Bob Shi)
Fixed: GITHUB-2886: testng-results xml reports config skips from base classes as ignored (Krishnan Mahadevan)

7.7.1
Fixed: GITHUB-2854: overloaded assertEquals methods do not work from Groovy (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void generateReport(
}
skipped += skippedPerTest;
retried += retriedPerTest;
ignored += testContext.getExcludedMethods().size();
ignored += testContext.getExcludedMethods().stream().filter(ITestNGMethod::isTest).count();
}
}

Expand Down
35 changes: 26 additions & 9 deletions testng-core/src/test/java/test/reports/XmlReporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.testng.internal.WrappedTestNGMethod;
import org.testng.reporters.RuntimeBehavior;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import test.SimpleBaseTest;
import test.reports.issue2171.TestClassExample;
import test.reports.issue2886.HydeTestSample;
import test.reports.issue2886.JekyllTestSample;
import test.simple.SimpleSample;

public class XmlReporterTest extends SimpleBaseTest {
Expand Down Expand Up @@ -60,7 +63,7 @@ public void ensureStackTraceHasLineFeedsTest() throws Exception {

@Test(description = "GITHUB-2171")
public void ensureCustomisationOfReportIsSupported() throws Exception {
File file = runTest(TestClassExample.class, "issue_2171.xml");
File file = runTest("issue_2171.xml", null, TestClassExample.class);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Expand All @@ -74,7 +77,6 @@ public void ensureCustomisationOfReportIsSupported() throws Exception {
public void ensureReportGenerationWhenTestMethodIsWrappedWithWrappedTestNGMethod() {
File file =
runTest(
SimpleSample.class,
testng ->
testng.setMethodInterceptor(
(methods, context) ->
Expand All @@ -88,19 +90,34 @@ public void ensureReportGenerationWhenTestMethodIsWrappedWithWrappedTestNGMethod
assertThat(file.exists()).isTrue();
}

private static File runTest(Class<?> clazz) {
return runTest(clazz, RuntimeBehavior.FILE_NAME, null);
@Test(description = "GITHUB-2886")
public void ensureConfigurationMethodsAreNotCountedAsSkippedInXmlReports() throws Exception {
File file =
runTest(RuntimeBehavior.FILE_NAME, null, JekyllTestSample.class, HydeTestSample.class);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.compile("/testng-results").evaluate(doc, XPathConstants.NODE);
int ignored = Integer.parseInt(node.getAttributes().getNamedItem("ignored").getNodeValue());
int total = Integer.parseInt(node.getAttributes().getNamedItem("total").getNodeValue());
int passed = Integer.parseInt(node.getAttributes().getNamedItem("passed").getNodeValue());
int failed = Integer.parseInt(node.getAttributes().getNamedItem("failed").getNodeValue());
assertThat(ignored).isZero();
assertThat(total).isEqualTo(2);
assertThat(passed).isEqualTo(2);
assertThat(failed).isZero();
}

private static File runTest(Class<?> clazz, Consumer<TestNG> customizer) {
return runTest(clazz, RuntimeBehavior.FILE_NAME, customizer);
private static File runTest(Class<?> clazz) {
return runTest(RuntimeBehavior.FILE_NAME, null, clazz);
}

private static File runTest(Class<?> clazz, String fileName) {
return runTest(clazz, fileName, null);
private static File runTest(Consumer<TestNG> customizer) {
return runTest(RuntimeBehavior.FILE_NAME, customizer, SimpleSample.class);
}

private static File runTest(Class<?> clazz, String fileName, Consumer<TestNG> customizer) {
private static File runTest(String fileName, Consumer<TestNG> customizer, Class<?>... clazz) {
String suiteName = UUID.randomUUID().toString();
File fileLocation = createDirInTempDir(suiteName);
TestNG testng = create(fileLocation.toPath(), clazz);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test.reports.issue2886;

import org.testng.annotations.BeforeSuite;

public class BaseClassSample {

@BeforeSuite
public void beforeSuite() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test.reports.issue2886;

import org.testng.annotations.Test;

public class HydeTestSample extends BaseClassSample {

@Test
public void rogueBehaviourTest() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.reports.issue2886;

import org.testng.annotations.Test;

public class JekyllTestSample extends BaseClassSample {
@Test
public void gentleBehaviourTest() {}
}

0 comments on commit 2e0c8bd

Please sign in to comment.