PAGE-7: PRESERVE ORDER IN TestNG

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 by default 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");
               }
 
}

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 1" preserve-order="fasle">
    <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.

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:



No comments:

Post a Comment

About Me

My photo
You can reach me out at : jimmiamrit@gmail.com

Total Pageviews