TestNG scenarios with priority including negative priority
In TestNG, we can use ‘priority’ to decide the order of the test case execution. The rules are :
1. If you are not setting any priority to a test case along with @Test annotation then TestNG will consider it as priority=0 by default.So, keeping with priority =0 and with default priority is not plan either keep without priority or priority=0. Both are same things.
2. Yes, we can set negative priority with test case in TestNG.
3. If you are setting negative priority for a test case then it will be executed firstly compare to test case having priority positive or without priority.
4. Yes, you can give two same priorities for many test cases then the execution will depend on the alphabetical order of name of the @Test methods.
You can find all scenarios below with priority test cases. This is a very important part of TestNG. If you are going for a Selenium WebDriver with TestNG interview, then you will be surely asked about the priority scenarios.
Create a TestNG class in your Eclipse and test all these cases-
Q 1.
@Test(priority=0)
public void test(){
System.out.println("low priority");
}
@Test(priority=1)
public void test1()
{
System.out.println("high priority");
}
Ans: low priority
High priority
Q. 2
@Test(priority=1)
public void test(){
System.out.println("low priority");
}
@Test(priority=0)
public void test1()
{
System.out.println("high priority");
}
Ans: high priority
Low priority
Q .3
@Test(priority=1)
public void test(){
System.out.println("low priority");
}
@Test(priority=1)
public void aest1()
{
System.out.println("high priority");
}
Ans:
high priority
low priority
PASSED: aest1
PASSED: test
Q 4.
@Test(priority= -1)
public void test(){
System.out.println("low priority");
}
@Test(priority=0)
public void test1()
{
System.out.println("high priority");
}
Ans:- low priority
high priority
PASSED: test
PASSED: test1
@Test(priority= -1)
public void test(){
System.out.println("low priority");
}
@Test(priority=-2)
public void test1()
{
System.out.println("high priority");
}
Ans :
high priority
low priority
PASSED: test1
PASSED: test
Q 5. No Priority, priority=-1
@Test
public void test(){
System.out.println("low priority");
}
@Test(priority=-1)
public void test1()
{
System.out.println("high priority");
}
Ans:
high priority
low priority
PASSED: test1
PASSED: test
Q.6
@Test
public void test(){ //no priority
System.out.println("test Priority=null");
}
@Test(priority=-1)
public void testn()
{
System.out.println("test Priority=Negative");
}
@Test(priority=0) //priority=0
public void test1()
{
System.out.println("test Priority=zero");
}
@Test(priority= 1) //priority=1
public void test3()
{
System.out.println("test Priority=1");
}
Ans:-
test Priority=Negative
test Priority=null
test Priority=zero
test Priority=1
PASSED: testn
PASSED: test
PASSED: test1
PASSED: test3
In TestNG, we can use ‘priority’ to decide the order of the test case execution. The rules are :
1. If you are not setting any priority to a test case along with @Test annotation then TestNG will consider it as priority=0 by default.So, keeping with priority =0 and with default priority is not plan either keep without priority or priority=0. Both are same things.
2. Yes, we can set negative priority with test case in TestNG.
3. If you are setting negative priority for a test case then it will be executed firstly compare to test case having priority positive or without priority.
4. Yes, you can give two same priorities for many test cases then the execution will depend on the alphabetical order of name of the @Test methods.
You can find all scenarios below with priority test cases. This is a very important part of TestNG. If you are going for a Selenium WebDriver with TestNG interview, then you will be surely asked about the priority scenarios.
Create a TestNG class in your Eclipse and test all these cases-
Q 1.
@Test(priority=0)
public void test(){
System.out.println("low priority");
}
@Test(priority=1)
public void test1()
{
System.out.println("high priority");
}
Ans: low priority
High priority
Q. 2
@Test(priority=1)
public void test(){
System.out.println("low priority");
}
@Test(priority=0)
public void test1()
{
System.out.println("high priority");
}
Ans: high priority
Low priority
Q .3
@Test(priority=1)
public void test(){
System.out.println("low priority");
}
@Test(priority=1)
public void aest1()
{
System.out.println("high priority");
}
Ans:
high priority
low priority
PASSED: aest1
PASSED: test
Q 4.
@Test(priority= -1)
public void test(){
System.out.println("low priority");
}
@Test(priority=0)
public void test1()
{
System.out.println("high priority");
}
Ans:- low priority
high priority
PASSED: test
PASSED: test1
@Test(priority= -1)
public void test(){
System.out.println("low priority");
}
@Test(priority=-2)
public void test1()
{
System.out.println("high priority");
}
Ans :
high priority
low priority
PASSED: test1
PASSED: test
Q 5. No Priority, priority=-1
@Test
public void test(){
System.out.println("low priority");
}
@Test(priority=-1)
public void test1()
{
System.out.println("high priority");
}
Ans:
high priority
low priority
PASSED: test1
PASSED: test
Q.6
@Test
public void test(){ //no priority
System.out.println("test Priority=null");
}
@Test(priority=-1)
public void testn()
{
System.out.println("test Priority=Negative");
}
@Test(priority=0) //priority=0
public void test1()
{
System.out.println("test Priority=zero");
}
@Test(priority= 1) //priority=1
public void test3()
{
System.out.println("test Priority=1");
}
Ans:-
test Priority=Negative
test Priority=null
test Priority=zero
test Priority=1
PASSED: testn
PASSED: test
PASSED: test1
PASSED: test3
nice
ReplyDelete