Enroll in Selenium Training

In the introduction to the TestNG tutorial, we highlighted TestNG briefly and focussed on how TestNG fetches its power from its annotations. Tests annotate to decide the sequence of the tests that run. Annotations are taken from the Java language and are an excellent tool for the testers using TestNG. This tutorial includes the TestNG Annotations with the following content:

  • What are TestNG annotations?
  • Benefits of TestNG annotations
  • Hierarchy of TestNG annotations
    • Multiple Test Case Scenario
  • TestNG Case Priorities In TestNG

What Are TestNG Annotations?

Annotations, in general, mean "a comment" or "a note"  on a diagram, etc. to denote its meaning. TestNG also uses them for the same reason. TestNG annotations are the code that is written inside your source test code logic to control the flow of the execution of tests. It is essential to annotate your methods in TestNG to run the tests. TestNG will ignore the method which does not contain an annotation since it won't know when to execute this method.

A TestNG annotation starts from the symbol "@" and whatever follows is the annotation name. These names are predefined, and we will discuss them in the later section of this tutorial. Apart from the '@' symbol and the header file (of course), there is nothing you require to run TestNG annotations. There are many types of annotations in TestNG. In the section, you will find their definition along with their meaning.

Types Of TestNG Annotations

In TestNG, there are ten types of annotations:

  • @BeforeSuite - The @BeforeSuite method in TestNG runs before the execution of all other test methods.
  • @AfterSuite - The @AfterSuite method in TestNG runs after the execution of all other test methods.
  • @BeforeTest - The @BeforeTest method in TestNG runs before the execution of all the test methods that are inside that folder.
  • @AfterTest - The @AfterTest method in TestNG executes after the execution of all the test methods that are inside that folder.
  • @BeforeClass - The @BeforeClass method in TestNG will run before the first method invokes of the current class.
  • @AfterClass - The @AfterClass method in TestNG will execute after all the test methods of the current class execute.
  • @BeforeMethod - The @BeforeMethod method in TestNG will execute before each test method.
  • @AfterMethod - The @AfterMethod method in TestNG will run after each test method is executed.
  • @BeforeGroups - The @BeforeGroups method in TestNG run before the test cases of that group execute. It executes just once.
  • @AfterGroups - The @AfterGroups method in TestNG run after the test cases of that group execute. It executes only once.

These annotations have self-explanatory meanings. It is one of the primary reasons to prefer TestNG as it is simple and easy to learn. If TestNG draws so much from its annotations, there must be a few benefits associated with it.

Why Use Annotations?

TestNG annotations boast the following benefits:

  • Easy To Learn - The annotations are very easy to learn and execute. There is no predefined rule or format, and the tester just needs to annotate methods using their judgment.
  • Can Be Parameterized - Annotations can also be parameterized, just like any other method in Java.
  • Strongly Typed- Annotations type strongly, and the errors can be encountered during the run time, which saves time for the testers.
  • No Need To Extend Any Class - While using the annotations, there is no need to extend any Test class like JUnit.

Now that we know the benefits and the annotations used, its time to use them in our code. But hey!, as I said, you control the flow of the program using these annotations. For this, we must know what test will execute first and what next. So before we jump onto the coding part, let's see the hierarchy of these annotations.

Hierarchy In TestNG Annotations

TestNG provides many annotations to write good test source code while testing software. So, how will TestNG figure out which test case to run first and then the next and so on? The answer is a hierarchy in these annotations. TestNG contains a hierarchy among the annotations. This hierarchy is as follows (top being the highest priority):

  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass
  • @BeforeMethod
  • @Test
  • @AfterMethod
  • @AfterClass
  • @AfterTest
  • @AfterSuite

To demonstrate the hierarchy, we have written a small code for you.

Now with the below example code, it will be clear to you quickly.

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 
 
public class TestNG {
 @Test
 public void testCase1() {
   System.out.println("This is the A Normal Test Case"); 
 }

 @BeforeMethod
 public void beforeMethod() {
   System.out.println("This will execute before every Method");
 }
 
 @AfterMethod
 public void afterMethod() {
   System.out.println("This will execute after every Method");
 }
 
 @BeforeClass
 public void beforeClass() {
   System.out.println("This will execute before the Class");
 }
 
 @AfterClass
 public void afterClass() {
   System.out.println("This will execute after the Class");
 }
 
 @BeforeTest
 public void beforeTest() {
   System.out.println("This will execute before the Test");
 }
 
 @AfterTest
 public void afterTest() {
   System.out.println("This will execute after the Test");
 }
 
 @BeforeSuite
 public void beforeSuite() {
   System.out.println("This will execute before the Test Suite");
 }
 
 @AfterSuite
 public void afterSuite() {
   System.out.println("This will execute after the Test Suite");
 }
}

Can you guess the output based on the hierarchy? Give it a thought before seeing the output below.

The output of the above code will be like this: testng_annotation_output

It is visible that the @Suite annotation is the very first and the very lastly executed. Then @Test followed by @Class. Now, if you notice, the @Method has run twice. As @Test is a method in the class, hence @Method

Multiple Test Case Scenario

Numerous test cases can run by setting the priority of the test in the test methods. How? Hold that thought as we will surely take it up later in the tutorial. But, what if we forget about the priorities for a second? What's the protocol for running multiple test cases in TestNG?

If there are multiple @Test cases, TestNG runs the test cases in the alphabetical order. So, a test as

@Test
public void alpha(){
}

will run before the following test case:

@Test
public beta(){
}

Test Priority in TestNG

Although TestNG annotations decide in which order the tests will run, priorities do more or less the same job.

The priorities are an additional option that we can put to use with the test annotations. This attribute decides the priority of the annotation. But remember that priority check happens after the annotation check by TestNG. So the TestNG annotation hierarchy is followed first and then priority-based execution. The larger the priority number, the lower is its priority. So a method with priority 1 will run after the test with priority 0.  A genuine question after learning this is, what if the priorities are the same for two methods? Let's see those.

TestNG Methods With Same Priorities

It might happen (intentionally or unintentionally) that the tester decides to provide the same priorities for different methods under TestNG annotations. In that case, TestNG runs the test cases in the alphabetical order. So the following test cases:

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

@Test(priority=1)
 public void a_method(){
    System.out.println("A method");
 }

Will have the following output: testng annotation priority output

That is, in alphabetical order.

Okay, I think you got it. Two tests with no priority will run alphabetically. Test cases with the same priority also run alphabetically. But, what about the combination of them?

TestNG Test Case With and Without Priority

This section will explain how the TestNG will execute the test cases with and without the priority option. For this, I will add two more methods to our previous code.

import org.testng.annotations.Test;
public class TestNG {
	@Test (priority = 1)
	public void b_method() {
		System.out.println("This is B method");
	}
		
	@Test (priority = 1)
	public void a_method() {
		System.out.println("This is A method");
	}
		
	@Test
	public void d_method() {
		System.out.println("This is D Method");
	}
		
	@Test
	public void c_method() {
		System.out.println("This is C Method");
	}
}

Execute the following code and see the output: testng cases with and without annotations

The test cases without the priority attribute are given the "priority" and executed before the methods with priority. Also, they run alphabetically. I hope the priority attribute is clear in TestNG annotations. It brings us to the end of the concepts of annotations. Annotations are the core of TestNG, and mastering them means mastering TestNG. So keep practicing and keep experimenting to learn. In the next tutorial, we will see the TestNG groups.

Common Questions

Can we use parameters in TestNG Annotations?
Yes, using parameters is a very common way to use annotations. Parameters can be used similarly to a method in Java. An example of using parameters along with the annotations would be:

public class ParameterInTestNG {

@Parameters({ "suite-param" })
@Test
   public void prameterTestOne(String param) {
   System.out.println("Test one suite param is: " + param);
}

We have a dedicated tutorial for this TestNG Parameters. You can get a clear explanation there.

Can we set priority manually in TestNG annotations?
Definitely yes! TestNG provides this feature of defining the priority as a parameter in its annotations. TestNG figures out the priority and which test to run with the help of this parameter.

public class TestNGFirstTest {
  @Test(priority = 2) // Second Highest Priority
   public void a_test() {
   }

  @Test(priority = 3) // Lowest Priority
  public void c_test() {
  }

  @Test(priority = 1) // Highest Priority
  public void b_test() {
  }
}

Are multiple parameters allowed in annotations?
Yes, you can use multiple parameters in the annotations.

TestNG Test Suite
TestNG Test Suite
Previous Article
TestNG Groups
TestNG Groups
Next Article

Similar Articles