How To Prioritise The Test Cases Using TestNG Priority keyword

Posted By : Manish Kumar | 30-Jan-2018

We all know basic use of TestNG and use it for Automation testing Script, but simply using TestNG @Test annotation has one issue in its by default settings.

 
@Test, by default, executes the tests in Alphabetic order. 
 
So, we need to use ordering of tests as per our requirement and we need to use a keyword "priority" to achieve this. 
We can assign the order of tests starting from zero(0).
Syntax for which can be given as: 
 
@Test(priority = o)
 
Let us look for  full example for prioritizing the test cases:
 
 

 

package exampleTestPriority; import org.openqa.selenium.WebDriver; import org.testng.annotations.Test; public class TestCases { public WebDriver driver; @Test(priority = 0) public void One_1() { System.out.println("Test Case number One(1)"); } @Test(priority = 1) public void Two_2() { System.out.println("Test Case number Two(2)"); } @Test(priority = 2) public void Three_3() { System.out.println("Test Case number Three(3)"); } @Test(priority = 3) public void Four_4() { System.out.println("Test Case number Four(4)"); } }

 
and output will show:

 

Test Case number One(1) Test Case number Two(2) Test Case number Three(3) Test Case number Four(4) Passed: One_1 Passed: Two_2 Passed: Three_3 Passed: Four_4

Thanks.

Related Tags

About Author

Author Image
Manish Kumar

Manish is Adaptive and passionate to learn new technical skills, have knowledge of testing and love to play badminton in free time

Request for Proposal

Name is required

Comment is required

Sending message..