Skip to main content
TestNG Annotations: The Complete Guide for Automation Testing
Last Updated May 26, 2020 — Continuous Testing Expert

TestNG Annotations: The Complete Guide for Automation Testing

Continuous Testing

An Overview of TestNG Mobile Automation Testing Framework

What is TestNG?

TestNG is a comprehensive automation testing framework that covers all testing models such as unit testing, integration testing, functional and end-to-end testing. Inspired by JUnit and NUnit, Cedric Beust created TestNG in 2004 to make end-to-end testing simple and easy. With annotations and reporting features, TestNG makes it easy to code test cases. TestNG is the short form of Test Next Generation.

The best thing with TestNG is the reporting feature. It offers a detailed test report showing how many test cases failed, how many skipped, and how many succeeded. When you run a method with multiple test cases and if one test case fails, you have to run the method again with all test cases. You cannot run the failed test alone. So, you will be running successful tests too. However, TestNG allows you to generate a failed test report as an XML file. It means you can run this XML file to run only failed test cases. You don't need to run successful ones again.

Another important feature is that you can group multiple test cases and run them according to the pre-defined sequence or run one case multiple times. TestNG allows parallel testing which means you can run multiple test cases on multiple platforms while easily integrating it with CI/CD environments such as Jenkins.

TestNG Annotations

Annotations in TestNG are a great feature that allows developers to easily understand the code while automatically handling exceptions. While multi-threaded testing is supported, runtime configuration is flexible and API comes as a plugin.

For the installation of TestNG and more please see our previous blog post. 

Step 1: Create a TestNG File

Open Eclipse and go to Package Explorer view. Select the test project you named earlier (williamtestng here)

Right-click on src and choose new ->other

TestNG Annotations

Now click on TestNG and it will display TestNG Class.

TestNG Annotations

On the next screen, it will prompt you to enter new TestNG class details.

TestNG Annotations

For the source folder, click on browser and choose the TestNG project.

TestNG Annotations

Provide the source folder and package information and choose annotations.

eg: @BeforeTest and @AfterTest and click finish.

Eclipse, will now create your TestNG template automatically.

TestNG Annotations

Step 2: Create a TestNG test case

Now, TestNG is ready for use. So, write a test case and run it in Eclipse. Here is an example of a TestNG test case.

  [java] import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import; import org.testng.Assert; import org.testng.annotations.Test; public class NewTest { public WebDriver driver = new ChromeDriver(); String appUrl = ""; @Test public void verifyHomepageTitle() { // launch the Chrome browser and open the url driver.get(""); // maximize the browser window driver.manage().window().maximize(); // declare and initialize the variable to store the expected title of the webpage. String expectedTitle = " Experitest: Mobile App & Cross-Browser Testing End-to-End"; // fetch the title of the web page and save it into a string variable String actualTitle = driver.getTitle(); Assert.assertEquals(expectedTitle,actualTitle); // close the web browser driver.close(); } } [/java]

An Overview of Java Annotations

Annotations make Java code easy to understand and debug. An annotation is a type of metadata that doesn't directly affect the annotated code operation but provides information about a program that is not included in the actual program. They help the compiler to easily detect bugs and errors while helping developers by suppressing warnings. Using Annotations, you can generate XML files or code.

Java supports two types of Annotations:

  1. Built-in Annotations: eg: @SupressWarnings, @Override
  2. Custom Annotations: The user can declare an annotation using the @interface element.

While creating custom Java annotations, make sure that the method doesn't have any parameter and it doesn't throw clauses. Moreover, the method should only return primitive data types, Strings, enum, array or Class.

Java supports three types of Annotations

  1. Market Annotation: The market annotation doesn't use any method.
  2. Single-Value Annotation: This annotation uses a single method.
  3. Multi-Value Annotation: This type of annotation uses multiple methods (more than 1)

Using Annotations in TestNG

TestNG is popular for its annotation features that are easy to understand and use. They are self-explanatory. Here are the available annotations in TestNG:

[table id=63 /]

Here is an example of how to use Annotations in TestNG:

[java] import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.*; public class williamtestngfile { public String baseUrl = ""; String driverPath = "C:\\geckodriver.exe"; public WebDriver driver ; @BeforeTest public void launchBrowser() { System.out.println(" Firefox Browser Launching"); System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.get(baseUrl); } @Test public void verifyHomepageTitle() { String expectedTitle = "Experitest: Mobile App & Cross-Browser Testing End-to-End"; String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle, expectedTitle); } @AfterTest public void terminateBrowser(){ driver.close(); } } [/java]
  • In the above example, the main test case about verifying the title page of the Experitest website is written with the @Test annotation.
  • Before running the main test case, you need to invoke the Firefox browser. So, browser launching function is written in the @BeforeTest method. So, this method is executed first.
  • After running the test case, the launched browser should be closed. This method is added in @AfterTest annotation which will be executed after @Test method is executed.
  • When you want to run a single test case multiple times, you can add priority values along with the @Test annotation. In that case, the lowest priority value method will be executed first.

In order to take a closer look at the process and try it for yourself, take a free trial of SeeTest today.

Guy Arieli - CTO


More from the Blog

View more
Apr 12, 2021

These blockers most commonly stop people from learning test automation

Continuous Testing
We are often asked if it is hard to become a test automation engineer. ...
Read More
Apr 05, 2021

Build better apps with a low-code development approach

Continuous Testing
From being an option, DevOps is quickly turning into a necessity for m ...
Read More
Mar 29, 2021

Every web and mobile app tester needs these 7 surprising software testing skills

Continuous Testing
If you look at a job listing for QA or test automation professionals, ...
Read More
Mar 24, 2021

Paving the way for better mobile and web application testing orchestration

Continuous Testing
Keeping applications up to date and ensuring they work with all of the ...
Read More
Contact Us