Search code examples
testngtestng.xml

TestNG execution order - it's mixing tests from different classes


TestNG mixes tests from different classes when executing. Each class has a few tests. And instead of executing like this:

  1. FirstTestClass firstTest
  2. FirstTestClass secondTest
  3. FirstTestClass thirdTest

  4. SecondTestClass firstTest

  5. SecondTestClass secondTest
  6. SecondTestClass thirdTest

It executes like this, mixing tests from each class:

  1. FirstTestClass firstTest
  2. SecondTestClass firstTest
  3. FirstTestClass secondTest
  4. SecondTestClass secondTest
  5. FirstTestClass thirdTest
  6. SecondTestClass thirdTest

This is my XML:

<suite name="Mobile App Automation" verbose="1">
<test name="Android">
    <parameter name="OS" value="android"/>
    <parameter name="remote" value="true"/>
    <classes>
        <class name="Test.FirstTestClass"/>
        <class name="Test.SecondTestClass"/>
    </classes>
</test>

All my tests have the priority parameter set. But it's supposed to affect only tests inside a class, not EVERY test of the project, which is happening now.

Any hints?


Solution

  • When your code runs from the testng file, all the test cases with priority=0 run first then run the tests with priority=1 and so on. So if you want the test cases to run in a particular order you need to remove the priorities from the tests from all the classes.
    And in the testng file you can also add preserve-order="true" along with the <suite name="Mobile App Automation" verbose="1"> line, then all the tests mentioned in the first class will run first and then the tests in the second class, but still if there is priorities set within the classes, the order of the tests will run according to the priorities.
    So you need to remove the priorities first and then you can use preserve-order="true" to maintain the order of execution of the classes.