Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure that the order specified in TestNG.xml is retained?

On using TestNG+Selenium , I'm not able to ensure the order of execution of classes.The order specified below (in testng.xml) is not working ->ClassTwo executes first and then ClassOne is executed.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ABC" parallel="">
  <test verbose="2" name="xyz" annotations="JDK" preserve-order="true">
    <classes>
      <class name="script.ClassOne"/>
      <class name="script.ClassTwo"/>
    </classes>
  </test>
</suite>

How can I ensure that the order specified in TestNG.xml is retained?

like image 275
Mandy Avatar asked Jun 14 '12 10:06

Mandy


People also ask

How do you preserve order in TestNG?

If you want your classes / methods to be run in an unpredictable order, then we should go for preserve-order attribute in testng. In TestNg bydefault the preserve-order attribute will be set to 'true', this means, TestNG will run your tests in the order they are found in the XML file.

What is the correct order in TestNG xml?

The hierarchy in the testng xml file is very simple to understand. Very first tag is the Suite tag<suite>, under that it is the Test tag<test> and then the Class tag<classes>.

What is the default order of execution of the test method in TestNG?

In such cases, TestNG follows an alphabetical order while executing them. The below example has two test methods (a and b), and both have the same default priority that is 0. Therefore, the order of execution will be method a followed by b.


2 Answers

You just have to set parallel value to none

<suite name="ABC" parallel="none">

it works for me !

like image 62
Nissrine Nakiri Avatar answered Oct 21 '22 02:10

Nissrine Nakiri


According to TestNG documentation:

By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the preserve-order attribute to false

I would suggest leaving the preserve-order attribute out, since it is set by default.

However, you have two other options to force specific order to the test methods/classes:

  1. Invoke tests programmatically.
  2. Implement method interceptor, that will order the list of the tests.
like image 39
artdanil Avatar answered Oct 21 '22 02:10

artdanil