Preserve Order in TestNG

Test Case: 1

package testng;

import org.testng.annotations.Test;

public class PreserveOrderCase1 
{
	@Test
	public void TestCase1()
	{
		System.out.println("Executing 1st Test Case");
	}
	@Test
	public void TestCase2()
	{
		System.out.println("Executing 2nd Test Case");
	}

}

Test Case: 2

package testng;

import org.testng.annotations.Test;

public class PreserveOrderCase2 
{
	@Test
	public void TestCase3()
	{
		System.out.println("Executing 3rd Test Case");
	}
	@Test
	public void TestCase4()
	{
		System.out.println("Executing 4th Test Case");
	}

}

Test Case: 3

package testng;

import org.testng.annotations.Test;

public class PreserveOrderCase3 
{
	@Test
	public void TestCase5()
	{
		System.out.println("Executing 5th Test Case");
	}
	@Test
	public void TestCase6()
	{
		System.out.println("Executing 6th Test Case");
	}

}

testng.xml (preserve-order=true > The tests will be executed in order defined in xml)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve Order Test Suite" parallel="classes" thread-count="2">
  <test name="Preserve Order Execution" preserve-order="true" >
    <classes>
      <class name="testng.PreserveOrderCase1"/>
      <class name="testng.PreserveOrderCase2"/>
      <class name="testng.PreserveOrderCase3"/>
    </classes>
  </test>
</suite>

Output: preserve-order=true

testng.xml (preserve-order=false > Test will not be executed in order. They will get executed in an unpredictable order)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve Order Test Suite" parallel="classes" thread-count="2">
  <test name="Preserve Order Execution" preserve-order="false" >
    <classes>
      <class name="testng.PreserveOrderCase1"/>
      <class name="testng.PreserveOrderCase2"/>
      <class name="testng.PreserveOrderCase3"/>
    </classes>
  </test>
</suite>

Output: preserve-order=false

Leave a Reply

Your email address will not be published. Required fields are marked *