TestNG is a Testing framework, that covers different types of test designs like a unit test, functional test, end to end test, UI test and integration test. You can run a single or multiple test cases in your Testng code.
If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a priority as zero(0).
Now, while running; lower priorities will be scheduled first.
Demo of TestNG code without Priority
Let us take a scenario where sequencing will be required in order to pass all test cases:
Scenario: Generate a code where you are required to perform a Google search with a specific keyword say “Facebook”. Now, verify that Browser title is changed to “Facebook – Google Search”.
Note: Each step which you code should be in separate methods
Method 1: Open Browser say chrome (openBrowser())
Method 2: Launch Google.com (launchGoogle())
Method 3: Perform a search using “Facebook” (performSearchAndClickFirstLink())
Method 4: Verify Google search page title (faceBookPageTitleVerification())
package firsttestngpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public
class PriorityDemo {
WebDriver driver;
// Method 1: Open Brower say Firefox
@Test
public
void openBrowser() {
driver = new ChromeDriver();
}
// Method 2: Launch Google.com
@Test
public
void launchGoogle() {
driver.get(“http://www.google.co.in”);
}
// Method 3: Perform a search using “Facebook”
@Test
public
void peformSeachAndClickFirstLink() {
driver.findElement(By.xpath(“.//*[@title=’Search’]”)).sendKeys(“Facebook”);
}
// Method 4: Verify Google search page title.
@Test
public
void faceBookPageTitleVerification() throws Exception {
driver.findElement(By.xpath(“.//*[@value=’Search’]”)).click();
Thread.sleep(3000);
Assert.assertEquals(driver.getTitle().contains(“Facebook – Google Search”), true);
}
}
Explanation of Code
As mentioned above we have created 4 test cases for performing each action in an independent method.
- The first method (openBrowser) states to initialize Firefox browser.
- The second method (launchGoogle) states that launch Google.com is in the initialized browser.
- The third method (peformSeachAndClickFirstLink)states that perform a search in the search box (with xpath (“.//*[@title=’Search’]”) with a search term as Facebook and
- The fourth and last method (faceBookPageTitleVerification) states that click on search icon of Google and verify that browser title has been changed to Facebook – Google Search.
Now run this code using testNG as shown in the video you will find all the Test Case are failing. The reason for failure: as there is a dependency of previous test case to pass, only than current running test case will be passed.
In this case,
- First method which is executed is openBrowser(). It got passed because it does not have any dependency.
- Second method executed is faceBookPageTitleVerification(); it is failing because we are trying to click search button and verifying browser title.
- You can see that if search activity is not process then how any other step can get passed. Hence, this is the reason my test cases are failing.
PASSED: openBrowser
FAILED: faceBookPageTitleVerification
FAILED: launchGoogle
FAILED: peformSeachAndClickFirstLink
Demo of TestNG code without Priority in Alphabetical Order
If we do not mention any priority, testng will execute the @Test methods based on alphabetical order of their method names irrespective of their place of implementation in the code.
package firsttestngpackage;
import org.testng.annotations.Test;
public
class PriorityDemo {
@Test
public
void method_c(){
System.out.println(“I’m in method C”);
}
@Test
public
void method_b(){
System.out.println(“I’m in method B”);
}
@Test
public
void method_a(){
System.out.println(“I’m in method A”);
}
@Test
public
void method_e(){
System.out.println(“I’m in method E”);
}
@Test
public
void method_d(){
System.out.println(“I’m in method D”);
}
}
Output of code
[RemoteTestNG] detected TestNG version 7.3.0
I’m in method A
I’m in method B
I’m in method C
I’m in method D
I’m in method E
PASSED: method_a
PASSED: method_b
PASSED: method_c
PASSED: method_d
PASSED: method_e
Though we defined the methods in a random manner (c, b, a, e, d), testng executed the methods based on their method names by considering alphabetical order and the same was reflected in the output as well.
How to set Priority in TestNG
As you have seen in the previous example that sequencing required in order to pass this scenario, so we will be modifying the previous piece of code with Priority Parameter so that each test should run against to the priority assigned to them.
Now as you can see we have assigned the Priority to each test case means test case will the lower priority value will be executed first.
package firsttestngpackage;
import org.testng.annotations.Test;
public
class PriorityDemo {
@Test(priority = 0)
public
void method_c(){
System.out.println(“I’m in method C”);
}
@Test(priority = 3)
public
void method_b(){
System.out.println(“I’m in method B”);
}
@Test(priority = 2)
public
void method_a(){
System.out.println(“I’m in method A”);
}
@Test(priority = 1)
public
void method_e(){
System.out.println(“I’m in method E”);
}
@Test(priority = 4)
public
void method_d(){
System.out.println(“I’m in method D”);
}
}
Output of code
[RemoteTestNG] detected TestNG version 7.3.0
I’m in method C
I’m in method E
I’m in method A
I’m in method B
I’m in method D
PASSED: method_c
PASSED: method_e
PASSED: method_a
PASSED: method_b
PASSED: method_d
Methods with Same Priority
There may be a chance that methods may contain same priority. In those cases, testng considers the alphabetical order of the method names whose priority is same.
package firsttestngpackage;
import org.testng.annotations.Test;
public
class PriorityDemo {
@Test(priority = 0)
public
void method_c(){
System.out.println(“I’m in method C”);
}
@Test(priority = 3)
public
void method_b(){
System.out.println(“I’m in method B”);
}
@Test(priority = 2)
public
void method_a(){
System.out.println(“I’m in method A”);
}
@Test(priority = 1)
public
void method_e(){
System.out.println(“I’m in method E”);
}
@Test(priority = 2)
public
void method_d(){
System.out.println(“I’m in method D”);
}
}
Output of code
[RemoteTestNG] detected TestNG version 7.3.0
I’m in method C
I’m in method E
I’m in method A
I’m in method D
I’m in method B
PASSED: method_c
PASSED: method_e
PASSED: method_a
PASSED: method_d
PASSED: method_b
Combining both prioritized(having same priority) and non-prioritized methods
In this case, we will cover two cases in one testng class.
- Methods having same priority value.
- More than one non-prioritized methods.
package firsttestngpackage;
import org.testng.annotations.Test;
public
class PriorityDemo {
@Test
public
void method_c(){
System.out.println(“I’m in method C”);
}
@Test(priority = 3)
public
void method_b(){
System.out.println(“I’m in method B”);
}
@Test
public
void method_a(){
System.out.println(“I’m in method A”);
}
@Test(priority = 1)
public
void method_e(){
System.out.println(“I’m in method E”);
}
@Test(priority = 1)
public
void method_d(){
System.out.println(“I’m in method D”);
}
}
Output of Code
[RemoteTestNG] detected TestNG version 7.3.0
I’m in method A
I’m in method C
I’m in method D
I’m in method E
I’m in method B
PASSED: method_a
PASSED: method_c
PASSED: method_d
PASSED: method_e
PASSED: method_b
First preference: Non-prioritized methods: ‘c’ and ‘a’: Based on alphabetical order ‘a’ was executed first and then ‘c’.
Second preference: Prioritized methods: ‘b’, ‘e’ and ‘d’, d and e have same priority so they will be executed based on alphabet and later b will be executed.
Case-sensitive in TestNG
Just for your information there is a standard syntax for defining priority in testNG i.e. @Test (priority=4), suppose you are defining it in some other syntax say @Test (PRIORITY=1) then your IDE will show it as a compilation error
Conclusion
As you have seen that if there is a requirement to run a set of test-case in specific sequence then it can be easily done using Priority using testNG as a run tool.
