@Test annotation attributes – priority attribute

In this post, we will look at how we can prioritize our test cases using TestNG so that some cases can execute before others. TestNG provides us with a priority attribute to help us achieve just that. We are going to look at the below points.

Let’s look at all of the above points one by one

Run test cases with no priority

What would be the order of the execution of test cases if we don’t define priority at all?

Here, the test cases will be executed alphabetically based on the names of the test methods. The default priority that is assigned to the test cases is 0. So, we can say that if we didn’t prioritize, all test cases have a priority value of 0 (which means all cases will have the same priority ).

Below is an example of the same –

public class CodekruTest {

	@Test
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test
	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test
	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}
}

Output after running the above test class –

Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
PASSED: alphaMethod
PASSED: betaMethod
PASSED: gammaMethod

The test methods are executed in the alphabetical order of their test method names.

Run test cases with no priority ( difference in method names by a smallcase and uppercase letter )

Earlier, we saw that if we don’t assign any priority, the methods execute in the alphabetical order of their test names. But what if the two test methods have the same name but differ only in the starting of the first letter being uppercase and lowercase?

Here the uppercase one will be executed first and then the lowercase one.

public class CodekruTest {

	@Test
	public void alphaMethod() {
		System.out.println("Executing alphaMethod having smallcase letter");
		Assert.assertTrue(true);
	}

	@Test
	public void AlphaMethod() {
		System.out.println("Executing AlphaMethod having uppercase letter");
		Assert.assertTrue(true);
	}
}

Output after running the above test class-

Executing AlphaMethod having uppercase letter
Executing alphaMethod having smallcase letter
PASSED: AlphaMethod
PASSED: alphaMethod

Now, how to set a test case priority?

We can set the test case priority by using the priority attribute of the @Test annotation. Lower priority number methods execute first, as shown in the below program.

public class CodekruTest {

	@Test(priority =  1)
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test( priority =  2)
	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test( priority = 3)
	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}
}

Output after executing the above Test class –

Executing gammaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
PASSED: gammaMethod
PASSED: alphaMethod
PASSED: betaMethod

The order of execution was dependent on the priority attribute number.

Remember, the default priority of a test method is 0. So, if we don’t assign any priority using the priority attribute, then the priority of that test method is 0.

public class CodekruTest {

	@Test(priority =  1)
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test
	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test( priority =  -1)
	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}
}

Output after executing the above test class –

Executing betaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
PASSED: betaMethod
PASSED: alphaMethod
PASSED: gammaMethod

TestNG executed cases in this specific order because

  • betaMethod has the priority -1
  • alphaMethod has the default priority ( which is 0 )
  • gammaMethod has the priority 1

Running test cases having the same priority

Now, what if we have two test cases having the same priority? What would be the order of execution then?

I know you would have guessed what would happen here. It’s the same scenario where we don’t assign any priority, and all of the test methods would get a priority of 0. So, if two test methods have the same priority, the execution order will also fall in the alphabetical order of their test names.

public class CodekruTest {

	@Test(priority =  1)
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test( priority =  2)
	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test( priority =  2)
	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}
}

Output after executing the above test class –

Executing gammaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
PASSED: gammaMethod
PASSED: alphaMethod
PASSED: betaMethod

So, what happened here?

  • gammaMethod was executed first because it had the lowest priority of all the test methods.
  • Then there was a conflict between alphaMethod and betaMethod because of their same priority. Still, alphaMethod won because it came earlier in the alphabetical ordering than betaMethod.
  • And then lastly, the betaMethod executes last.

priority + dependsOnMethods combination

What would happen if we use both priority and dependsOnMethods in a way that both will contradict each other?

Here, we will keep the priority of the depending(betaMethod) method lower than that of the dependent method(alphaMethod), as shown in the below program.

public class CodekruTest {

	@Test(priority =  1)
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test(priority =  3)
	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	@Test( priority =  2, dependsOnMethods = {"alphaMethod"})
	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}
}

Here the priority of the alphaMethod is 3, but that of the betaMethod is 2. So, betaMethod should execute before alphaMethod. But as betaMethod depends on the alphaMethod for its execution as defined by the dependsOnMethods attribute, alphaMethod would be executed before betaMethod.

Output after running the above Test class –

Executing gammaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
PASSED: gammaMethod
PASSED: alphaMethod
PASSED: betaMethod

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Other @Test attributes
Liked the article? Share this on

Leave a Comment

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