How to maintain order of execution as mentioned in TestNG.xml?


In latest version, TestNG executes the classes in order as mentioned in TestNG.xml or any executable XML file. However, sometimes orders are not intake while running the tesng.xml in older version. TestNG may run the classes in random order.In latest version, TestNG executes the classes in order as mentioned in TestNG.xml or any executable XML file. However, sometimes orders are not intake while running the tesng.xml in older version. TestNG may run the classes in random order.

In this article, we will discuss how to make sure order of execution remain intake as per tesng.xml

TestNG supports 2 attribute − parallel and preserve−order. These attributes are used in testing.xml

  • Parallel attribute is used to run tests in parallel. So, 1st check is to make sure execution is not happening in parallel to retain order of execution. Parallel attribute is mentioned at suite level so add parallel = "none" instead of parallel="".

  • Preserver−order attribute is used at test level. TestNG assigns the value by default as false in older jars. And it can cause the random execution. To make sure, order of execution is intake, we assign the attribute as true in testing.xml

Now, we will see how to implement these two attributes to make sure order of execution is retained as per testing.xml

Approach/Algorithm to solve this Problem

  • Step 1: Create 2 TestNG classes −OrderofTestExecutionInTestNG and NewTestngClass

  • Step 2: Write 2 different @Test method in each of these 2 classes − OrderofTestExecutionInTestNG and NewTestngClass.

  • Step 3: Now create the testNG.xml to run these 2 TestNG classes and mention parallel=none and preserve−order=true as given below

  • Step 4: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

The following code for TestNG class − OrderofTestExecutionInTestNG:

import org.testng.annotations.*;

public class OrderofTestExecutionInTestNG {
    // test case 1
    @Test
    public void testCase1() {
        System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
    }
    // test case 2
    @Test
    public void testCase2() {
        System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
    }
 }   

The following code for common TestNG class − NewTestngClass:

import org.testng.annotations.*;

public class NewTestngClass {

    @Test
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
    @Test
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
    }
} 

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1" parallel = "none">
   <test name = "test1" preserve-order = "true">
      <classes>
         <class name = "OrderofTestExecutionInTestNG"/>
    <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

in test case 1 of OrderofTestExecutionInTestNG
in test case 2 of OrderofTestExecutionInTestNG
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass

===============================================
Suite1
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
=============================================== 

Updated on: 16-Aug-2023

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements