简体   繁体   中英

TestNg running multiple suites running multiple times

I have a high level XML file: testsng.xml

<suite name="allSuites">
  <suite-files>
    <suite-file path="tests.run1.xml" />
  </suite-files>
</suite>

My intention is to parse this XML file using the org.testng.xml.Parser add some info to it.

  Parser parser = new Parser(inputXml);
  parser.setLoadClasses(false);
  Collection<XmlSuite> suites = parser.parse();
  XmlSuite parentSuite = suites.iterator().next();   

Then once the structure has all necessary tests/class, will pass it back to TestNG:

  TestNG testng = new TestNG();
  List<XmlSuite> suitesList = new ArrayList<XmlSuite>();
  suitesList.add(parentSuite);
  testng.setXmlSuites(suitesList);
  testng.run();

The problem I'm seeing is that tests.run1.xml is getting run twice (when I only expect it to run once) and not even letting the first run finish:

[TestNG] Running:
tests.run1.xml
...
[TestNG] Running:
tests.run1.xml
...
[TestNG] Running:
testsng.xml

I am not sure if this is supposed to be regarded as a bug in TestNG or if this is supposed to be treated as Working As Designed

The problem is that you are invoking the parsing logic explicitly to construct the List<XmlSuite> and then passing it into TestNG. But since the XmlSuite list that you created, was parsed via the TestNG parser, it adds details of the suite file name into it.

Now when you plug in this XmlSuite into TestNG, TestNG internally parses it again and thus causes your Child Test suite to be created once again. That explains your child suite running twice.

If all you want is to enrich/modify the contents of your suite file, then here's a cleaner way of doing it.

  • Ensure you are on TestNG v6.11 or higher.
  • Build a TestNG listener which implements org.testng.IAlterSuiteListener and within its alter() method, you iterate through the List<XmlSuite> and add your enrichment logic.
  • You now inject this listener either via <listeners> tag (or) via the @Listeners annotation (or) via Service Loaders. You can refer more about it here .

I have logged this as a bug in TestNG. Please see here for bug details.

You can refer to my answer here on StackOverflow for a sample on how to use the IAlterSuiteListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM