Web Testing Automation with TestNG

Table of Contents

TestNG stands for “Test Next Generation. TestNG is a Java-based testing framework. That covers various types of testing like Unit, Functional, end-to-end, and integration testing. TestNG allows the tester to write the test cases and execute them efficiently. And generate detailed test reports. The tester can easily come to know how many test cases are passed, failed, skip and the tester can execute the failed test cases separately.

You can install the TestNG plug-in and run your test-cases from the Eclipse

Steps to install TestNG plug-in:

  • Lunch the Eclipse IDE
  • Click on the help menu
  • Now you can see the Eclipse Marketplace click on that
  • Search the TestNG and Install & restart the Eclipse IDE

TestNG Install

Advantages of TestNG

  • User-Friendly: TestNG is straightforward to learn and implement.
  • Rich API: It offers numerous classes, interfaces, and methods that facilitate testers' work.
  • Annotations: These are intuitive and enable parallel testing. They help produce HTML reports and facilitate easier test grouping.
  • Data-Driven Testing: Supports @DataProvider for data-driven tests.
  • Tool Integration: Compatible with various tools and plugins.
  • Dependency Management: Allows defining dependencies between test methods.
  • Priority Setting: Test methods can be assigned priorities.
  • Test Grouping: Enables grouping of test methods for organized testing.
  • Parameterization: Supports parameterizing test cases using @Parameters annotations.
  • Java Integration: Acts as a compiler for Java code, eliminating the need for a main method in the test classes.

Contact Us For Automation Testing Services

You can add TestNG.XML file:

  • Lunch the Eclipse IDE
  • Right click on your project
  • Go to the TestNG > Convert to TestNG and Finish.

Convert to TestNG

TestNG Annotations

Annotations are metadata added to the source code to control test execution. Key annotations include:

  • @BeforeSuite: Runs once before all tests in the suite.
  • @AfterSuite: Executes after all tests in the suite.
  • @BeforeTest: Runs before any @Test method within the <test> tag in the XML file.
  • @AfterTest: Runs after all @Test methods within the <test> tag.
  • @BeforeClass: Executes once before the first @Test method in the current class.
  • @AfterClass: Executes after all the test methods in the current class.
  • @BeforeMethod: Runs before each @Test method.
  • @AfterMethod: Runs after each @Test method.
  • @BeforeGroups: Executes before the first test method in a specific group.
  • @AfterGroups: Executes after all test methods in a specific group.

Here is the use of @BeforeMethod Annotations

use of BeforeMethod

Using TestNG Annotations

Here's an example demonstrating the use of these annotations:

public class TestNGAnnotations { 
   public void beforeSuite() { 
      System.out.println("Before Suite"); 
   public void afterSuite() { 
      System.out.println("After Suite"); 
   public void beforeTest() { 
      System.out.println("Before Test"); 
   public void afterTest() { 
      System.out.println("After Test"); 
   public void beforeClass() { 
      System.out.println("Before Class"); 
   public void afterClass() { 
      System.out.println("After Class"); 
   public void beforeMethod() { 
      System.out.println("Before Method"); 
   public void afterMethod() { 
      System.out.println("After Method"); 
   public void test1() { 
      System.out.println("Test Case 1"); 
   public void test2() { 
      System.out.println("Test Case 2"); 

These annotations make Selenium test scripts more manageable and efficient, providing significant assistance to testers.

Using the TestNG XML file you can create a testSuit and test-cases:TestNG XML file

Mobile Automation For Responsive Design Testing

Assertions in TestNG

Assertions verify if the expected results match the actual outcomes, helping determine test pass or fail status.

Type of Assertions

  • Hard Assert: Stops test execution immediately upon assertion failure.
  • Soft Assert: Collects all assertion errors but does not stop test execution, allowing multiple assertions in a test.

Common TestNG Assert Methods

  • Assert.assertEquals(String actual, String expected): that means Verifies if the actual and expected strings are equal.
  • Assert.assertEquals(String actual, String expected, String message): that means Displays a message upon assertion failure.
  • Assert.assertEquals(boolean actual, boolean expected): that means Checks equality of two boolean values.
  • Assert.assertTrue(condition): that means Asserts that a condition is true.
  • Assert.assertTrue(condition, message): that means Asserts that a condition is true, with a message upon failure.
  • Assert.assertFalse(condition): that means Asserts that a condition is false.
  • Assert.assertFalse(condition, message): that means Asserts that a condition is false, with a message upon failure.
  • Assert.assertEquals(Object actual, Object expected, String message): that means It checks the equality of two objects with an optional message.
  • Assert.assertEquals(String actual, String expected, String message): Assert Equals compare two values are equal or not.

Assertions are indeed fundamental in automation testing. They serve as checkpoints within test scripts to verify whether the actual outcomes align with the expected outcomes. By validating these outcomes, assertions help ensure the reliability and accuracy of the testing process.


The annotations in TestNG streamline test execution by allowing testers to control the order and grouping of tests easily. They also facilitate parallel execution and provide detailed test reports, making it simpler to identify and rerun failed test cases. This helps in maintaining a high standard of code quality and ensures thorough testing coverage. Its integration with various tools and plugins further enhances its capabilities, making it an asset for any testing team aiming to achieve high-quality software deliverables.

About Author

Rahul-Mahadik.pngAjay Makwana started his career in June 2019. He started his career as a trainee and is now a QA Executive in The One Technologies - a load and performance testing company.

He learned task prioritization when he was a junior QA, which helped me in his career. In the future, he wants to develop expertise in automation, database, and performance testing. In his free time, he loves long drives and watching cricket.