Here we learn how to set the priority in TestNG to execute the test cases on the basis of the priority.Let suppose we have n number of test cases and we want to specific test cases first and then the other test cases , in that case we need to define the priority in Test case.
Syntax to define the priority
@Test(priority = 1 )
public void Testcase1(){
}
Explanation : here we have defined the priority of testcase 1 is “1”;
Important Facts about Priority
1) By default the priority of the test case is “0” which means if we do not define any priority in test case it priority is set to “0”.
2) Priority can be from negative to positive.( It can be -10 or 10 ).
3) Test case having lower priority will execute first.
example : suppose we have 2 test cases – Test_Case1( Priority = 1 ) and TestCase2 ( Priority = 5 ) then Test_Case1 will execute first as it is having the lower priority.
4) If all the Test case have same priority that test cases run on the basis of ASCII values of name of test cases , whose ASCII value is less will execute first.( Test cases executes alphabetically ). Lets have an example to understand this.
Default execution of Test cases in TestNG
Lets create test cases to send en email , For this we make 3 test cases namely :
1) Test Case 1 : Login – login to the account
2) Test Case 2 : Compose_email – compose email
3) Test Case 3 : Send_email – send email
Now we execute all these test cases in TestNG and see the output and this is very important TestNg interview question that how test cases executes by default ?
Java Program
package com.TestNGTutorial; import org.testng.annotations.Test; public class Priority_Tutorial { // we have 3 test cases here to send an email // 1st test case to login in to the account @Test public void Login() { System.out.println("Test Case login execute : Login Successful"); } // 2nd Test case to compose email @Test public void Compose_mail() { System.out.println("Test case compose email excuted : Composing Mail"); } // 3rd Test case to send email @Test public void Sent_mail() { System.out.println("Test Case mail sending excuted : Mail Sent"); } }
Output – :
// Test case compose email excuted : Composing Mail Test Case login execute : Login Successful Test Case mail sending excuted : Mail Sent PASSED: Compose_mail PASSED: Login PASSED: Sent_mail =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Failures: 0, Skips: 0 ===============================================
Now if you see the above output we can see that no priority is defined for any test cases means all having same default priority 0 , then Compose_email execute 1st , Login execute 2nd and Sent_mail executes 3rd.
Here the Test cases execute on the basis of ASCII values of test case names
C has lowest ASCII value , then L and then S – ( you can also say that test cases execute alphabetically )
Note : Capital letter have lower ASCII value than small case letters.
Lets have an example to understand this : –
package com.TestNGTutorial; import org.testng.annotations.Test; public class Priority_Tutorial { // 1st test case to login in to the account @Test public void Login() { System.out.println("Test Case login execute : Login Successful"); } // 2nd Test case to compose email @Test public void compose_mail() { System.out.println("Test case compose email excuted : Composing Mail"); } }
Output :
// Test Case login execute : Login Successful Test case compose email excuted : Composing Mail PASSED: Login PASSED: compose_mail =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 0 ===============================================
Note : here in above program that Login test case executes first and compose email 2nd -> This is because capital letter “L” has lower ASCII value than small letter “c”. So first capital letter test case will execute then small letter test case will execute.
Priority in TestNG
Now as we know to send an email , we need first to login then compose email and then send email. so we set the priority in such a way to execute the test cases like this.
Java Code – we have set the priority now
lower priority test cases executes first – analyze the below program
package com.TestNGTutorial; import org.testng.annotations.Test; public class Priority_Tutorial { // we have 3 test cases here to send an email // 1st test case to login in to the account // priority 1 - executes first @Test(priority=1) public void Login() { System.out.println("Test Case login execute : Login Successful"); } // 2nd Test case to compose email // priority 2 - executes 2nd @Test(priority=2) public void compose_mail() { System.out.println("Test case compose email excuted : Composing Mail"); } // 3rd Test case to send email // priority 3 - executes 3rd @Test(priority=3) public void Sent_mail() { System.out.println("Test Case mail sending excuted : Mail Sent"); } }
Output – as per the priority test cases executed – priority 1 executes 1st then priority 2 executes then priority 3 executes
Test Case login execute : Login Successful Test case compose email excuted : Composing Mail Test Case mail sending excuted : Mail Sent PASSED: Login PASSED: compose_mail PASSED: Sent_mail =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Failures: 0, Skips: 0 ===============================================
Lets have one more example to understand priority in test cases in testNG –
Here we have 4 test case -Java program
package com.TestNGTutorial; import org.testng.annotations.Test; public class Priority_Tutorial { // 1st test case // priority -10 - lowest priority executes first @Test(priority=-10) public void A() { System.out.println("Test Case A executed"); } // 2nd Test case // priority 0 - default priotity - executes 2nd @Test public void H() { System.out.println("Test Case H executed"); } // 3rd Test case // priority 3 - and test case starts with P executes 3rd @Test(priority=3) public void P() { System.out.println("Test Case P executed"); } // 4th test case // priority 3 - and test case starts with Z executes last @Test(priority=3) public void Z() { System.out.println("Test Case Z executed"); } }
Output :
Test Case A executed Test Case H executed Test Case P executed Test Case Z executed PASSED: A PASSED: H PASSED: P PASSED: Z =============================================== Default test Tests run: 4, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 4, Failures: 0, Skips: 0 ===============================================
Explanation :
Test case A – executes first as it has the lowest priority
Test case H – executes 2nd as it has default priority zero as it executes 2nd
Test case P – executes 3rd as Test case “P” and Test case “Z” have same priority 3 so “P” comes before “Z” alphabetically so it executes 3rd before Z ( P has lower ASCII value than Z )
Test case Z – executes 4th as it comes after P and have same priority as P has.
This is all about Priority in TestNG and now you are clear with priority concept. Hope you like this article.