Saturday, September 28, 2019

Post#119.Preserve Order in Testng


Preserve Order in Testng
Preserve Order in TestNG will all about the order of test case execution 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.
Let me explain this with below example.

First of all configure your Project Structure as below.
Step 1: Create package = "PreserveOrder" and under this package Create classes as 
Register.java, Login.java, and LogOut.java
Once done with above steps your Project structure will look like this.


Register.java
package PreserveOrder;

import org.testng.annotations.Test;

public class Register {
  @Test
  public void TestCase() {
              System.out.println("===Register TestCase Executed===");
  }
}

Login.java
package PreserveOrder;
import org.testng.annotations.Test;

public class Login {
  @Test
  public void TestCase() {
              System.out.println("===Login TestCase Executed===");
  }
}

LogOut.java
package PreserveOrder;

import org.testng.annotations.Test;

public class LogOut {
  @Test
  public void TestCase() {
              System.out.println("===LogOut TestCase Executed===");
  }
}

Now we will define the 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 Test" preserve-order="false">
    <classes>
      <class name="PreserveOrder.Register"/>
      <class name="PreserveOrder.Login"/>
      <class name="PreserveOrder.LogOut"/>
      </classes>
  </test>
</suite>

Output:
[TestNG] Running:
  D:\WorkSpace\DemoTestNG\testng.xml

===LogOut TestCase Executed===
===Login TestCase Executed===
===Register TestCase Executed===

===============================================
Preserve order test runs
Total tests run: 3, Failures: 0, Skips: 0
===============================================
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.
You can see the above output test execution sequence; it is not same as given In testng.xml file.
Now let us use preserve-order="true" attribute in testng.xml file as bellow and observe execution result.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Preserve order test runs">
  <test name="Regression Test" preserve-order="true">
    <classes>
             <class name="PreserveOrder.Register"/>
      <class name="PreserveOrder.Login"/>
      <class name="PreserveOrder.LogOut"/>
    </classes>
  </test>
</suite>

OutPut:

[TestNG] Running:
  D:\WorkSpace\DemoTestNG\testng.xml

===Register TestCase Executed===
===Login TestCase Executed===
===LogOut TestCase Executed===

===============================================
Preserve order test runs
Total tests run: 3, Failures: 0, Skips: 0
===============================================
As you see the above output, Test is executed in order as configured in testng.xml file.

So this is the example of using preserve-order attribute in testng.xml file. To execute test cases in correct order, you need to set preserve-order=true and to execute them in unpredictable order, set preserve-order=false.


2 comments:

  1. A clear and understable. Cool keep doing buddy 😊

    ReplyDelete
  2. I have doubt. Suppose If we do not use preserve-order attribute in testing.xml file then what should be the default order?

    ReplyDelete