testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.98k stars 1.02k forks source link

Same xmlsuite is added multiple times when tests are run using TestNG.SetXmlSuites method #863

Open virenv opened 8 years ago

virenv commented 8 years ago

@cbeust,

Here is the description of the problem.

My trimmed down run engine code

    Parser parser = new Parser(".\\CompleteSuite.xml");
    List<XmlSuite> xmlSuite = null;
    try {
        xmlSuite = parser.parseToList();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // List<String> suites = new ArrayList<String>();
    TestNG testng = new TestNG();
    TestListener listner = new TestListener();
    testng.addListener(listner);
    testng.addListener(new SuiteListner());
    // suites.add(".\\CompleteSuite.xml");
    testng.setXmlSuites(xmlSuite);
    // testng.setTestSuites(suites);
    testng.setParallel("parallel");
    testng.setSuiteThreadPoolSize(5);
    testng.setDataProviderThreadCount(5);
    testng.setOutputDirectory("path to output");
    testng.run();

My suite XML looks like this

<suite name="suite of suites">
    <suite-files>
        <suite-file path="TestNG.xml" />
        <suite-file path="TestNG2.xml" />
    </suite-files>
</suite>

you can have any two testNG suites as mentioned here.

Problem When you run this code you will get an exception stating Two suites cannot have the same name: Suite2

This exception will come even if you have two different suite names. The reason is because of this code in initializeSuitesAndJarFile of TestNG class

    for (String suiteFile : s.getSuiteFiles()) {
        try {
            Collection<XmlSuite> childSuites = getParser(suiteFile).parse();
            for (XmlSuite cSuite : childSuites){
                cSuite.setParentSuite(s);
                s.getChildSuites().add(cSuite);
            }
        }

here do we really need adding of same suite back to the parent? s.getChildSuites().add(cSuite);

Virender

juherr commented 8 years ago

Could you share the content of TestNG.xml and TestNG2.xml?

virenv commented 8 years ago

TestNG1 - >

<suite name="Suite2" verbose="1"  parallel="methods">
  <test name="test002" >
    <classes>
       <class name="TestClasses.TestClass002" />
    </classes>
  </test>
</suite>

TestNG2 ->

<suite name="Suite1" verbose="1" parallel="methods" thread-count="1">
  <test name="test001" >
    <classes>
       <class name="TestClasses.TestClass001" />
    </classes>
  </test>
</suite>
virenv commented 8 years ago

Hi juherr,

I am on testng 6.9.4. I would have tried the latest version but saw that this part of the code hasn't changed.

virenv commented 8 years ago

@juherr, Does it look like a bug?

juherr commented 8 years ago

Yes, it looks like. Would you like to try to fix it? First thing to do is adding a test that reproduces the issue.

virenv commented 8 years ago

Let me take a look at it and will validate my understanding of the issue with the community.

Is there any documentation which talks about running TestNG unit tests on local machine?

juherr commented 8 years ago

running TestNG unit tests on local machine?

Just run testng.xml suite in IDE, or use the build tool you prefer between gradle, maven or kobalt.

virenv commented 8 years ago

Alright, let me put this up and I will send a PR for it.

virenv commented 8 years ago

It seems that this issue(behavior) was modified as a part of #829

juherr commented 8 years ago

In fact, I think #829 is just hiding the issue as it allows to have duplicate name. But in our case, "suite of suites", Suite1" and Suite2" are not duplication.

Now, I think you have another issue: some suites are run twice. Could you confirm?

virenv commented 8 years ago

@juherr

Yes suites are now run twice. Let me send a PR and lets discuss the impact of the fix with some code.

virenv commented 8 years ago

@cbeust @juherr in the context of this issue, the core problem is that in #829 we introduced a check to allow multiple suites with same name. I think it will be explicit that a suite with same name will be actually pointing to the same set of test. If this is the case we will have test sanity check that will not allow same test names in a run

 /**

in testng.java

Am i correct with my understanding? if this is the case then the change in#829 might not have the desired functionality.

I will send a PR with my assumption and lets see how it goes.

juherr commented 8 years ago

I think the solution should not be to check what is duplication or what is not, but find why TestNG is adding the same suite many times and fix it.

virenv commented 8 years ago

@juherr This fix and the fix in #829 are related. Here is the PR, if we put this code we will not be able to have multiple suite files in a run. There by rendering the fix in 829 unusable

vazotov commented 6 years ago

Please check my solution for making your TestNG suites more DRY https://stackoverflow.com/a/51666801/4165898