Yuvraj
Yuvraj

Reputation: 79

TestNG XML execution order

I have testng java project as below (in the exact order)

TestNGOnePack
 ClassOne.java
 ClassTwo.java

TestNGThreePack
 ClassOne.java
 ClassTwo.java

TestNGTwoPack
 ClassOne.java
 ClassTwo.java

each java file is as follows

@Test
public void pkgName()
{
    System.out.println(this.getClass().getCanonicalName());
}

testng.xml is as follows

<suite name="Suite One">
<test name="Test One" >
<classes>
 <class name="TestNGTwoPack.ClassTwo" />   
 <class name="TestNGOnePack.ClassOne" />
</classes>
    <packages>
         <package name="TestNGThreePack" />
   </packages> 
</test> 

when I ran this as testng I got the following output

TestNGTwoPack.ClassTwo
TestNGThreePack.ClassOne
TestNGOnePack.ClassOne
TestNGThreePack.ClassTwo

My question is the output's sequence is ambiguous Shouldn't it be as follows? what wrong here ??? what's the reason that the output is as above and not as below ??

TestNGTwoPack.ClassTwo
TestNGOnePack.ClassOne
TestNGThreePack.ClassOne
TestNGThreePack.ClassTwo

Upvotes: 3

Views: 19149

Answers (2)

Ran Adler
Ran Adler

Reputation: 3707

In the main xml order between the classes, if you need order inside the class set priority

on testng new version (6.14.*) it didnt work ,it work for me in tesng version 6.9.10

by package is not working :(

 <packages>
            <package name="com.a.automation.catedor.tests.Tests.*" />        
 </packages>    




    Main xml

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="All" verbose="10" parallel="false">

        <suite-files>
            <suite-file path="A.xml"/>
            <suite-file path="B.xml"/>
            <suite-file path="C.xml"/>
        </suite-files>
    </suite>

    ---A.xml ---

     <test name="General">
                <groups>
                    <run>
                        <exclude name="ignore"/>
                    </run>
                </groups>
                <classes>

                    <class name="com.a.automation.tests.ws.Class1"/>
                    <class name="com.a.automation.tests.ws.Class2"/>


                </classes>
        </test>

Upvotes: 0

drkthng
drkthng

Reputation: 6939

No, unfortunately what you see is the currently implemented behaviour in testNG.

See this discussion to stay up to date.

Workaround

There are several methods to work around this issue, including groups and dependsOnGroups etc. that you can read about in the discussion above.


But in your case I think I would go for several suite-files and one "Master-Suite" that fires these suites, then you can be sure to have the right order of execution.

Your testng_packageAandB.xml:

<?xml version="1.0" encoding="UTF-8"?>

<suite name="Suite - package A and B">
    <test name="Test - package A and B">
        <classes preserve-order="true">
            <class name="drkthng.selenium_experiments.packageB.Class01" />
            <class name="drkthng.selenium_experiments.packageA.Class02" />
        </classes>
    </test>
</suite>

Your testng_packageC.xml:

<?xml version="1.0" encoding="UTF-8"?>

<suite name="Suite - package C">
    <test name="Test - package C">
        <packages preserve-order="true">
            <package name="drkthng.selenium_experiments.packageC" />
        </packages>
    </test>
</suite>

And your "run all" xml:

<?xml version="1.0" encoding="UTF-8"?>

<suite name="allSuites">
  <suite-files>
    <suite-file path="testng_packageAandB.xml" />
    <suite-file path="testng_packageC.xml" />
  </suite-files>
</suite>

Running only the last xml will give you an execution that you expect, in my case this one:

[TestNG] Running:
D:\JavaWorkspace\selenium-experiments\testng_packageAandB.xml

drkthng.selenium_experiments.packageB.Class01 drkthng.selenium_experiments.packageA.Class02

[TestNG] Running:
D:\JavaWorkspace\selenium-experiments\testng_packageC.xml

drkthng.selenium_experiments.packageC.Class01 drkthng.selenium_experiments.packageC.Class02

Upvotes: 5

Related Questions