Table of contents
1.
Introduction
2.
TestNG annotations
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

TestNG Annotations in selenium

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Source

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);
   }
}
You can also try this code with Online Java Compiler
Run Code


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.

Frequently Asked Questions

  1. What is the TestNG framework?
    TestNG is an open-source testing framework used to automate tests. It makes end-to-end testing easy and generates accurate and elaborated test reports. It can be used to support other frameworks or IDEs to generate reports.
     
  2. What are annotations?
    The annotations are statements or lines of code that control the test methods' behavior. They are defined before every method in a test class to define the behavior. 
     
  3. What happens if we do not define any annotation before methods?
    The annotations 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. 
     
  4. What are the default annotations in TestNG?
    The default annotation of TestNG is @Test annotation, which is used to tell the compiler that the method is a test method. The methods that are defined with @Test annotation are executed in Lexicographic order.
     
  5. Why do we need to integrate TestNG with selenium?
    TestNG makes end-to-end testing easy and generates accurate and elaborated test reports. It supports selenium to generate test reports as selenium does not have the feature within it.

Key Takeaways

We have discussed the TestNG framework and its annotations in this article. Now you can perform tests using the annotations discussed in this article.

Hey Ninjas! We hope this blog helped you understand the TestNG framework better. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass