Preserver 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.

We will try to execute the below example, by taking three classes. For the first one, We will set the preserve-order attribute to false and check for the the Output.

Create three classes as
ClassOne.java
ClassTwo.java
ClassThree.java

package com.pack.preserve;
import org.testng.annotations.Test;

public class ClassOne {

	@Test
	public void firstTestCase() {
		System.out.println("im in first test case from ClassOne Class");
	}

	@Test
	public void secondTestCase() {
		System.out.println("im in second test case from ClassOne Class");
	}

}
package com.pack.preserve;
import org.testng.annotations.Test;

public class ClassTwo {

	@Test
	public void firstTestCase() {
		System.out.println("im in first test case from ClassTwo Class");
	}

	@Test
	public void secondTestCase() {
		System.out.println("im in second test case from ClassTwo Class");
	}

}
package com.pack.preserve;
import org.testng.annotations.Test;

public class ClassThree {

	@Test
	public void firstTestCase() {
		System.out.println("im in first test case from ClassThree Class");
	}

	@Test
	public void secondTestCase() {
		System.out.println("im in second test case from ClassThree Class");
	}

}

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, Now we will define testng.xml file with preserve-order attribute for tests and set the value as 'false'.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve order test runs">
  <test name="Regression 1" preserve-order="false">
    <classes>
      <class name="com.pack.preserve.ClassOne"/>
      <class name="com.pack.preserve.ClassTwo"/>
      <class name="com.pack.preserve.ClassThree"/>
    </classes>
  </test>
</suite>

As we have set the preserve-order attribute to false, test will not be executed in order. They will get executed in an unpredictable order. We have ClassOne, ClassTwo and ClassThree defined in xml, but the order that they executed are ClassOne, ClassThree, and ClassTwo. Check the output below:

Preserve Order False

Now we will define the xml file with preserve-order attribute for tests and set the value as 'true' (which is by default).

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve order test runs">
  <test name="Regression 1" preserve-order="true">
    <classes>
      <class name="com.pack.preserve.ClassOne"/>
      <class name="com.pack.preserve.ClassTwo"/>
      <class name="com.pack.preserve.ClassThree"/>
    </classes>
  </test>
</suite>

As we have set the preserve-order attribute to true, the test will be executed in order defined in xml. Check the output below:

Preserve Order True

Test Frameworks: 

Comments

When I run this from Eclipse it works perfectly fine. But when the testng.xml file is invoked from command line the order of execution of classes runs in alphabetical order. What should be done to avoid it? TIA.

By default preserve-order is set to 'False', which executes your tests in Alphabetical order of your test/method names.

When you set preserve-order to True, then your tests are executed in the order specified in your testng.xml file.

Add new comment