Introduction
TestNG is an open-source testing framework used to automate tests. It is an enhanced version of JUnit and NUnit. NG stands for Next Generation in TestNG. It makes end-to-end testing easy and generates accurate and elaborated test reports. TestNG is distributed under the Apache Software license.
Selenium does not have the feature to generate proper tests and reports alone. It needs support from any framework or third-party tools to achieve this. So TestNG is the best to use with selenium rather than other frameworks as it provides various features. It uses annotation like all the other testing frameworks to represent tests and their behavior. Let’s learn about annotations and the features further in this article.
TestNG annotations
The annotations are statements or lines of code that control the test methods' behavior. The annotations provided by TestNG are more advanced than all the other frameworks. They are defined before every method in a test class to define the behavior. If we do not provide annotation for any method, the test will be ignored, and the report will not be generated for those tests. We use @Test to represent a method as a test method. Let’s learn more about the annotations from the few listed below.
Annotation | Description |
---|---|
@Test | It tells the compiler that the method is a test method. |
@BeforeSuite | It tells the compiler that this test should be executed before all the tests in the suite. |
@BeforeTest | It tells the compiler that this test method should be executed before all the methods within the same class. |
@AfterTest | It tells the compiler that this test method should be executed after all the methods within the same class. |
@AfterSuite | It tells the compiler that this test should be executed after all the tests in the suite |
@BeforeClass | It tells the compiler that this test method should be executed before the first test method within the same class. |
@AfterClass | It tells the compiler that this test method should be executed after the last method within the same class. |
The workflow of the methods with these test annotations is shown below.

Let’s learn how to write test methods using the annotations listed above.
import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGExample{
@Test
Public void multiply() {
Assert.assertEquals(6, 3*2);
}
@AfterClass
public void subtract(){
Assert.assertEquals(11, 20-9);
}
@BeforeClass
public void add() {
Assert.assertEquals(18, 9+9);
}
}
We imported the annotations, assertions, and selenium in the above code. We created a class TestNGExample with methods multiply(), add(), and subtract().
These methods have the annotations @test, @AfterClass, and @BeforeClass, respectively.
- The method add() will be executed first because we defined it with the annotation@BeforeClass, and the test case will be passed successfully as the assertion evaluates to true (9+9 = 18).
- The method multiply() will be executed now as it has the @Test annotation, and the test case will be passed successfully as the assertion evaluates to true (3*2 = 6).
-
Finally, the method subtract() will be executed in the end as it has the @AfterClass annotation, and the test case will be passed successfully as the assertion evaluates to true (20-9 = 11).
All the test cases will be passed successfully because the assertions are evaluated to be true. This means the working logic of our code is correct and executes well.