2

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?

Max
  • 85
  • 1
  • 10

1 Answers1

1

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.

Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • But on a another project we have that same configuration and it doesn't behave like this. Is that possible? How are we able to set a priority for tests inside a given class and not according to the entire project? – Max Feb 01 '19 at 19:49
  • 1
    That’s not possible because testng behaviour is consistent and i have also faced the same issue in the past. When the tests are executed from the testng xml, tests are run according to the priorities on the whole instead of just the class level. Is there any specific reason that you need priorities because if some tests within a class are dependent on each other then you can use the dependsOn parameter within the class as well which would also solve the purpose of running the execution in a specific order. But if your all tests are independent then you need to remove the priorities. – Sameer Arora Feb 01 '19 at 19:54
  • Thanks Sameer. What I finally did was to create different tags in the XML, for each test class I wanted to execute. And it works now as I expected. – Max Feb 01 '19 at 20:23
  • Great, happy to help you. Please upvote and accept the answer if it helped you :) – Sameer Arora Feb 01 '19 at 20:24
  • @Max thanks for accepting the answer, though i would still suggest to remove the priorities from the tests and if your tests are dependent on each other then use dependsOn parameter, because in future your number of tests will increase and that would make difficult for you to enter each and every test in your testng xml, mentioning just the class name would be much easier. – Sameer Arora Feb 02 '19 at 06:30