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

Fix issue 2866 #2878

Merged
merged 2 commits into from
Mar 6, 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-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)
Fixed: GITHUB-2796: Option for onAfterClass to run after @AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ public Object clone() {
result.setExcludedGroups(getExcludedGroups());
result.setJUnit(isJUnit());
result.setParallel(getParallel());
result.setThreadCount(getThreadCount());
result.setVerbose(getVerbose());
Map<String, String> localParameters = new HashMap<>();
localParameters.putAll(getLocalParameters());
Expand Down
124 changes: 123 additions & 1 deletion testng-core/src/test/java/org/testng/xml/XmlSuiteTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package org.testng.xml;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import org.testng.ITestNGListener;
import org.testng.TestNG;
import org.testng.TestNGException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import org.testng.xml.XmlSuite.ParallelMode;
import org.testng.xml.github1533.SuiteCounter;
import org.testng.xml.github1874.TestClassSample;
import org.testng.xml.internal.Parser;
import org.testng.xml.issue2866.ATestClassSample;
import org.testng.xml.issue2866.BTestClassSample;
import org.testng.xml.issue2866.ThreadCountingSuiteAlteringListener;
import test.SimpleBaseTest;

public class XmlSuiteTest extends SimpleBaseTest {
Expand Down Expand Up @@ -109,8 +118,121 @@ public void ensureSuiteLevelBeanshellIsAppliedToAllTests() throws IOException {
}
}

@Test(description = "GITHUB-2866")
public void ensureSuiteLevelThreadCountsAreInheritedInTestTags() {
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName("Sample_suite");
xmlSuite.setThreadCount(1);
xmlSuite.setParallel(ParallelMode.CLASSES);
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName("Sample_test");
xmlTest.setClasses(
Arrays.asList(new XmlClass(ATestClassSample.class), new XmlClass(BTestClassSample.class)));
xmlTest.setThreadCount(5);
xmlTest.setParallel(ParallelMode.CLASSES);
TestNG testng = create(xmlSuite);
ThreadCountingSuiteAlteringListener listener = new ThreadCountingSuiteAlteringListener();
testng.addListener(listener);
testng.run();
assertThat(listener.getThreadIds("Sample_test_cloned")).hasSize(2);
}

@Test(description = "GITHUB-1533")
public void testScenarioWithChildSuites() {
String suiteFile = "src/test/resources/xml/github1533/parent.xml";
runTests(suiteFile, 1, 2, "GitHub1533_Suite", "GitHub1533_Parent_Suite");
}

@Test(description = "GITHUB-1533")
public void testScenarioWithNoChildSuites() {
String suiteFile = "src/test/resources/xml/github1533/child.xml";
runTests(suiteFile, 0, 1, "GitHub1533_Suite");
}

@Test(description = "GITHUB-1874")
// We are explicitly not including any assertions here because the intent of this test is to
// ensure that there are no exceptions triggered related to circular dependency.
public void testEnsureNoCyclicDependencyTriggered() {
XmlSuite suite = createXmlSuite("sample_suite");
XmlTest test = createXmlTest(suite, "sample_test");
XmlClass entryOne = new XmlClass(TestClassSample.class);
createXmlInclude(entryOne, "testMethodTwo");
test.getClasses().add(entryOne);
XmlClass entryTwo = new XmlClass(TestClassSample.class);
createXmlInclude(entryTwo, "testMethodOne");
test.getClasses().add(entryTwo);
TestNG testng = create(suite);
testng.run();
}

/**
* This test checks that TestNG can handle duplicate child suites when we have the following set
* of files:
*
* <p>- parent-suite-with-duplicate-child -> [child-suite-1, children/child-suite-3, child-suite1,
* children/child-suite-3] - children/child-suite-3 -> [../child-suite-2, child-suite-4,
* morechildren/child-suite-5]
*
* <p>SHOULD return a XmlSuite object with following structure:
*
* <p>parent-suite-with-duplicate-child ├───child-suite-1 ├───child-suite-3 │ ├───child-suite-2 │
* ├───child-suite-4 │ └───child-suite-5 ├───child-suite-1(0) └───child-suite-3(0)
* ├───child-suite-2(0) ├───child-suite-4(0) └───child-suite-5(0)
*
* <p>but NOT like:
*
* <p>parent-suite-with-duplicate-child ├───child-suite-1 ├───child-suite-3 ├───child-suite-1(0)
* └───child-suite-3(0) ├───child-suite-2 ├───child-suite-4 ├───child-suite-5 ├───child-suite-2(0)
* ├───child-suite-4(0) └───child-suite-5(0)
*
* <p>Check the <code>checksuitesinitialization</code> folder under test resources
*/
@Test(description = "GITHUB-1850")
public void checkDuplicateChildSuites() throws IOException {
String path =
getPathToResource("checksuitesinitialization/parent-suite-with-duplicate-child.xml");
Parser parser = new Parser(path);
List<XmlSuite> suites = parser.parseToList();
XmlSuite rootSuite = suites.get(0);
assertEquals(rootSuite.getChildSuites().size(), 4);

XmlSuite suite3 = rootSuite.getChildSuites().get(1);
assertEquals(suite3.getName(), "Child Suite 3");
assertEquals(suite3.getChildSuites().size(), 3);

XmlSuite suite3_0 = rootSuite.getChildSuites().get(3);
assertEquals(suite3.getName(), "Child Suite 3");
assertEquals(suite3_0.getChildSuites().size(), 3);

XmlSuite suite5 = suite3.getChildSuites().get(2);
assertEquals(suite5.getName(), "Child Suite 5");
assertEquals(suite5.getTests().size(), 1);

XmlSuite suite5_0 = suite3_0.getChildSuites().get(2);
assertEquals(suite5_0.getName(), "Child Suite 5");
assertEquals(suite5_0.getTests().size(), 1);
}

private static void runTests(
String suiteFile, int childSuitesCount, int suiteCounter, String... suiteNames) {
List<XmlSuite> suites;
try {
suites = new Parser(suiteFile).parseToList();
} catch (IOException e) {
throw new TestNGException(e);
}
assertEquals(suites.size(), 1);
assertEquals(suites.get(0).getChildSuites().size(), childSuitesCount);
TestNG testng = create(suites);
SuiteCounter listener = new SuiteCounter();
testng.addListener((ITestNGListener) listener);
testng.run();
assertEquals(listener.getCounter(), suiteCounter);
assertThat(listener.getSuiteNames()).containsExactly(suiteNames);
}

static class StringOutputStream extends OutputStream {
private StringBuilder string = new StringBuilder();
private final StringBuilder string = new StringBuilder();

@Override
public void write(int b) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test.suites.github1533;
package org.testng.xml.github1533;

import org.testng.annotations.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test.suites.github1533;
package org.testng.xml.github1533;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test.suites.github1874;
package org.testng.xml.github1874;

import org.testng.annotations.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.testng.xml.issue2866;

import org.testng.annotations.Test;

public class ATestClassSample {

@Test
public void fictitiousTest() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.testng.xml.issue2866;

import org.testng.annotations.Test;

public class BTestClassSample {

@Test
public void fictitiousTest() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.testng.xml.issue2866;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.testng.IAlterSuiteListener;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class ThreadCountingSuiteAlteringListener
implements IInvokedMethodListener, IAlterSuiteListener, ITestListener {

private final Map<String, Set<Long>> mappings = new ConcurrentHashMap<>();

@Override
public void alter(List<XmlSuite> suites) {
XmlSuite xmlSuite = suites.get(0);
XmlTest xmlTest = xmlSuite.getTests().get(0);
XmlTest clonedTest = (XmlTest) xmlTest.clone();
clonedTest.setName(xmlTest.getName() + "_cloned");
}

@Override
public void onStart(ITestContext context) {
mappings.put(context.getName(), Collections.synchronizedSet(new HashSet<>()));
}

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
Set<Long> threadIds = mappings.get(testResult.getTestContext().getName());
threadIds.add(Thread.currentThread().getId());
}

public Set<Long> getThreadIds(String testName) {
return mappings.get(testName);
}
}

This file was deleted.

This file was deleted.

27 changes: 0 additions & 27 deletions testng-core/src/test/java/test/suites/github1874/IssueTest.java

This file was deleted.

8 changes: 0 additions & 8 deletions testng-core/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -908,14 +908,6 @@
</classes>
</test>

<test name="Suite_Related_Tests">
<classes>
<class name="test.suites.github1533.Github1533Test"/>
<class name="test.suites.github1850.DuplicateChildSuitesInitializationTest"/>
<class name="test.suites.github1874.IssueTest"/>
</classes>
</test>

<test name="Jar_Related_tests">
<classes>
<class name="org.testng.JarFileUtilsTest"/>
Expand Down
Loading