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

TestNG.xml doesn't honour Parallel value of a clone. #2866

Closed
1 of 7 tasks
kamal-kaur04 opened this issue Jan 10, 2023 · 7 comments · Fixed by #2878
Closed
1 of 7 tasks

TestNG.xml doesn't honour Parallel value of a clone. #2866

kamal-kaur04 opened this issue Jan 10, 2023 · 7 comments · Fixed by #2878
Labels
Milestone

Comments

@kamal-kaur04
Copy link

kamal-kaur04 commented Jan 10, 2023

TestNG Version

Currently, I'm using listener via ServiceLoader.

Listener

public class TestNgAlterSuiteListener implements IAlterSuiteListener {
    @Override
    public void alter(List<XmlSuite> suites) {
        XmlSuite suite = suites.get(0);
        XmlTest xmlTest = suite.getTests().get(0);

        List<XmlTest> newXmlTests = new ArrayList<>();

        XmlTest newXmlTest = (XmlTest) xmlTest.clone();
        newXmlTest.setName("Name_1");
        newXmlTest.setXmlClasses(xmlTest.getXmlClasses());
        newXmlTests.add(newXmlTest);

        System.out.println(newXmlTest.getParallel());  // it returns correct value i.e., classes
        suite.setTests(newXmlTests);
    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test name="Test" thread-count="5" parallel="classes">
		<classes>
			<class name="com.A" />
			<class name="com.B" />
		</classes>
	</test> <!-- Test -->
</suite> <!-- Suite -->

Expected behavior

The two classes should run in parallel.

Actual behavior

But these classes run sequentially.

Is the issue reproducible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

Please, share the test case (as small as possible) which shows the issue

Contribution guidelines

Incase you plan to raise a pull request to fix this issue, please make sure you refer our Contributing section for detailed set of steps.

@krmahadevan
Copy link
Member

@kamal-kaur04 - Would you be willing to help raise a PR to fix this ? You just need to add the changes to https://github.com/cbeust/testng/blob/master/testng-core-api/src/main/java/org/testng/xml/XmlTest.java#L418

@kamal-kaur04
Copy link
Author

kamal-kaur04 commented Jan 10, 2023

@krmahadevan But I can see result.setParallel(getParallel()); value in clone() method

@krmahadevan
Copy link
Member

@kamal-kaur04 - Yeah my bad. I missed seeing that. But I am not sure i still understand the problem. Here's a sample that I used to reproduce this and I am not able to.

ClassA and ClassB look exactly like this
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class ClassA {

  @Test
  public void foo() {
    ITestResult itr = Reporter.getCurrentTestResult();
    String msg = String.format("<%s> test case %s running on Thread # %s",
        itr.getTestContext().getName(), itr.getMethod().getQualifiedName(),
        Thread.currentThread().getId());
    System.err.println(msg);
  }
}
My listener
import java.util.List;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class SuiteChanger implements IAlterSuiteListener {

  @Override
  public void alter(List<XmlSuite> suites) {
    XmlTest original = suites.get(0).getTests().get(0);
    XmlTest cloned = (XmlTest) original.clone();
    cloned.setName("cloned_test");
  }
}
My Sample test case
import java.util.Arrays;
import java.util.Collections;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlSuite.ParallelMode;
import org.testng.xml.XmlTest;

public class SampleTestCase {

  public static void main(String[] args) {
    XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setName("sample_suite");
    XmlTest original = new XmlTest(xmlSuite);
    original.setName("original_test");
    original.setParallel(ParallelMode.CLASSES);
    original.setClasses(Arrays.asList(new XmlClass(ClassA.class), new XmlClass(ClassB.class)));
    xmlSuite.setListeners(Collections.singletonList(SuiteChanger.class.getName()));
    TestNG testng = new TestNG();
    testng.setXmlSuites(Collections.singletonList(xmlSuite));
    testng.setVerbose(2);
    testng.run();
  }

}

Here's the output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
<original_test> test case com.rationaleemotions.issue2866.ClassA.foo running on Thread # 17
<original_test> test case com.rationaleemotions.issue2866.ClassB.foo running on Thread # 18
PASSED: com.rationaleemotions.issue2866.ClassA.foo
PASSED: com.rationaleemotions.issue2866.ClassB.foo

===============================================
    original_test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

<cloned_test> test case com.rationaleemotions.issue2866.ClassA.foo running on Thread # 19
<cloned_test> test case com.rationaleemotions.issue2866.ClassB.foo running on Thread # 20
PASSED: com.rationaleemotions.issue2866.ClassA.foo
PASSED: com.rationaleemotions.issue2866.ClassB.foo

===============================================
    cloned_test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
sample_suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

@kamal-kaur04
Copy link
Author

I think I was setting threadCount as 1 at SuiteLevel that's why it was getting inherited by particular test.

Any reason why suite Level thread-count takes priority if Test already has thread-count?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="1">
	<test name="Test" thread-count="5" parallel="classes">
		<classes>
			<class name="com.A" />
			<class name="com.B" />
		</classes>
	</test> <!-- Test -->
</suite> <!-- Suite -->

@krmahadevan
Copy link
Member

That is because the clone() method is missing out on setting the threadcount at the XmlTest level, and since now there's no valid value defined for thread count at the XmlTest level, TestNG honours the value set at the XmlSuite level.

@kamal-kaur04
Copy link
Author

kamal-kaur04 commented Jan 10, 2023

Ack, I'll share a PR for the fix. Thanks @krmahadevan

@krmahadevan
Copy link
Member

Sure. Please don't forget the following

  1. Add a test
  2. Add an entry into CHANGES.txt

Some more help on contributing to TestNG https://github.com/cbeust/testng/blob/master/.github/CONTRIBUTING.md#working-with-the-codebase

krmahadevan added a commit to krmahadevan/testng that referenced this issue Mar 6, 2023
@krmahadevan krmahadevan mentioned this issue Mar 6, 2023
3 tasks
@krmahadevan krmahadevan added the xml label Mar 6, 2023
@krmahadevan krmahadevan added this to the 7.8.0 milestone Mar 6, 2023
krmahadevan added a commit that referenced this issue Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants