Test execution priority in Selenium TestNG

Hello everyone,

Few days ago I was taking a look over a class which contained many @Test methods (from org.testng.annotations of course :) ) and whenever it was running, I was getting a different order of the tests in the results’ summary.

So this got me thinking and a quick solution came to mind: test dependency (dependsOnMethods / dependsOnGroups) but this would then imply that if the initial test fails, the execution would stop right then and there. I then found out about test method priority and it seems to be exactly what I was looking for and very easy to use.

Given the following example let’s see how we can obtain a desired order of execution: A > B > C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Test(priority = 1)
    public void testC() {
        System.out.println("Test C");
    }

    @Test(priority = 2)
    public void testA() {
        System.out.println("Test A");
    }

    @Test
    void testB() {
        System.out.println("Test B");
    }

In the above case, we get this order of execution: B > C > A which isn’t exactly what we wanted, let’s see how we can change that.

A few things need to mentioned about test method priorities:

  • lowest priority (can also be negative) will be executed first, thus -5 will run before priority 1
  • when no priority is specified, the default priority is 0, in our example, testB has the default priority

Since we’re aiming to obtain the A -> B ->C flow, we should use, the lowest priority for testA and set B and C to 1 and 2 respectively:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Test(priority = 2)
    public void testC() {
        System.out.println("Test C");
    }

    @Test
    public void testA() {
        System.out.println("Test A");
    }

    @Test(priority = 1)
    void testB() {
        System.out.println("Test B");
    }

Leave a Reply

Your email address will not be published. Required fields are marked *