- Default execution order of @Test methods
- Priority attribute usage
- Priority = 0
- Priority = -1 (negative priority)
- Same priority to all test methods
Default execution order of @Test methods
Ensure that you have TestNG installed.
If TestNG is not installed, than perform following steps: Help > Eclipse Marketplace…
Enter Testng in search field as shown below
Complete remaining steps to complete TestNG installation.
Ensure testng dependency in pom.xml file
Create a new class and add 5 @Test methods as shown below
Execute the Test
Notice the below output
The @Test methods get executed in alphabetical order (random manner).
So, by default, all the @Tests methods inside a class will be executed in alphabetical order.
Priority attribute usage
We can specify our own order of execution as well. This can be done using ‘priority’ attribute in TestNG.
So let us assign priority to all the @Test methods as shown below
Execute.
The @Test methods execution happens in order
Priority = 0
If we set priority=0 for a @Test method, the method will execute before the method having priority=1 (since 0 comes before 1).
Let us set priority=0 for testTwo() as shown below
Execute the test.
Notice below that “testTwo method” is printed before “testOne method” since former has priority=0
Priority = -1 (negative priority)
When we set priority=-1 for any @Test method, that method will be executed before the method that has priority=0 (since -1 comes before 0).
Let us set priority=-1 for testThree() as shown below
Execute.
Notice below that “testThree method” is printed before “testTwo method” since former has priority=-1
Same priority to all test methods
Let us give same priority to all the @Test methods
Setting same priority to all the @Test methods is same as not setting any priority to and @Test.
So when we execute, the result will be same like we saw for default execution order of @Test methods, see below
So this is how priority attribute can be used in TestNG.
The source code snapshot:
Thanks.