Priority
The priority for test method. Lower priorities will be scheduled first.
When there are multiple test cases, we want to execute test cases in particular order, we need add Priority attribute for @Test annotation
By default, priority is ‘0’.
Note: if you are not mentioned any priority to @Test annotation it will take by default is ‘0’, that is the case it will execute sequentially(means one after another)
Lets create a java class without Priority attribute
package testngexp;
import org.testng.annotations.Test;
publicclass priorityexample {
@Test
publicvoid TestMethod1(){
System.out.println("This TestMethod1");
}
@Test
publicvoid TestMethod2(){
System.out.println("This TestMethod2");
}
@Test
publicvoid TestMethod3(){
System.out.println("This TestMethod3");
}
}
TestNG.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<testname="Test1">
<classes>
<classname="testngexp.priorityexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Here, TestMethods are executed sequentially because we are not using any priority attribute.
Lets create a java class with priority attribute in @Test annotation
package testngexp;
import org.testng.annotations.Test;
publicclass priorityexample {
@Test(priority=3)
publicvoid TestMethod1(){
System.out.println("This TestMethod1");
}
@Test(priority=2)
publicvoid TestMethod2(){
System.out.println("This TestMethod2");
}
@Test(priority=1)
publicvoid TestMethod3(){
System.out.println("This TestMethod3");
}
}
TestNG.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<testname="Test1">
<classes>
<classname="testngexp.priorityexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Here, TestMethod3 is executed first because priority is given as 1 (always take least priority first) then TestMethod2 then TestMethod1.
Lets another example
package testngexp;
import org.testng.annotations.Test;
publicclass priorityexample {
@Test
publicvoid TestMethod1(){
System.out.println("This TestMethod1");
}
@Test(priority=2)
publicvoid TestMethod2(){
System.out.println("This TestMethod2");
}
@Test(priority=1)
publicvoid TestMethod3(){
System.out.println("This TestMethod3");
}
}
TestNG.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<testname="Test1">
<classes>
<classname="testngexp.priorityexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Here, TestMethod1 is executed first, even though we given priority attribute for other TestMethods (For TestMethod3 as priority 1 and TestMethod2 as priority 2) because if you are not defining any priority for @Test annotation it will take by default ‘0’ and execute those test cases first, in this scenario testMethod1 we are not define any priority, so it will take as priority 0 by default.
Comments
Post a Comment