top of page

How to Automate TestNG in Selenium

 Selenium is a testing framework to test specifically the UI of the application how it behaves on browser. TestNG is a testing framework to test the unit, functional, E2E, integration testing of the application.


TestNG is a popular testing framework for Java that allows users to perform automated testing for web applications. Selenium, on the other hand, is a popular automation testing tool that enables users to automate web browsers. Combining these two tools allows developers to create powerful automated tests for their web applications. Automating TestNG in Selenium involves setting up the environment, creating test cases, configuring TestNG, and integrating Selenium with TestNG.


What is TestNG Framework?


TestNG is an open-source test automation framework for Java. It is developed on the same lines as JUnit and NUnit. TestNG is an automation testing framework in which NG stands for “Next Generation”. TestNG is inspired by JUnit which uses the annotations (@). TestNG overcomes the disadvantages of JUnit and is designed to make end-to-end testing easy.

Created by Cedric Beust, it is used more frequently by developers and testers in test case creation owing to its ease of using multiple annotations, grouping, dependence, prioritization, and parametrization features.


Advantages of TestNG over Junit


·       Test cases can be grouped and prioritized as per need basis.

  • Annotations are easier to understand than Junit.

  • It produces the HTML reports for implementation.

  • It also generates the Logs.

  • In TestNG, there is no constraint available such as @beforeclass and @afterclass which is present in Junit.

  • TestNG enables you to group the test cases easily which is not possible in JUnit.

  • TestNG supports three additional levels such as @Before/After suite, @Before/AfterTest, and Before/AfterGroup.

  • TestNG does not extend any class. TestNG framework allows you to define the test cases where each test case is independent of other test cases.

  • It allows you to run the test cases of a particular group. Let's consider a scenario where we have created two groups such as 'Smoke' and 'Regression'. If you want to execute the test cases in a 'Regression' group, then this can only be possible in the TestNG framework.

  • Parallel execution of test cases, i.e., running multiple test cases is only possible in the TestNG framework.


Why use TestNG with Selenium?

One of the drawbacks of Selenium is that it does not have a proper format for the test results. By using the TestNG framework in Selenium, you can:

·     Generate the report in a proper format.

·     Include the number of test cases run; tests passed, failed, and skipped in the report.

·     Group test cases by converting them to testing.xml

·     Use invocation count and execute multiple tests without using loops.

·     Perform cross browser testing.

·     Easily understand annotations.


Installing and Setting up TestNG



It’s very easy to install TestNG. If you are using Eclipse IDE, it comes as a plugin. Below are the steps to install TestNG:

1.Install Eclipse IDE from the Eclipse website. It serves as a platform for you to code on and write your test cases

Once installed, go to help and navigate to the ‘Eclipse Marketplace’. The referenced snapshot is below: 



2.Click on ‘Eclipse Marketplace’. You will be directed to the marketplace modal. Type TestNG in the search keyword and hit ‘Go’. The referenced snapshot is below:




3.If TestNG is not installed in your Eclipse, rather than the ‘Installed’ button you would see ‘install’. Click on install and your TestNG framework will be installed in your Eclipse. Eclipse would recommend you to restart to use the features of the installed plugin

Post-restarting your Eclipse, re-verify whether you can see options for creating a TestNG class or not as below: 


TestNG Annotations


An annotation tag provides information about the method, class, and suite. It helps to define the execution approach of your test cases and the different features associated with it. Below are the major annotations used.

·     @TestThis is the root of TestNG test cases. To use TestNG, all methods should be annot

·     ated with this annotation. Below is an example


public void setupTestNG()


System.out.println(“this is a test annotation method”)


A few attributes associated with the TestNG annotation are:

Description: You can describe your test case under the description, stating what it does

@Test(description=”This test validates login functionality”)

Priority: You can prioritize the order of your test methods in TestNG by defining a priority. Based on the defined priority, the test shall execute in that order.


DependsOnMethod: This attribute works if one test case is dependent on another. For example, to view your profile details, you need to login to the application. So, your profile test case is dependent on the login test case


Enabled: Using this attribute, you can choose to execute or skip the execution of this test case. Setting it to true execute it and putting it to false will skip the test from the execution cycle


Groups: Using this attribute, you can club your test cases into a single group and specify the group you wish to execute in your TestNG XML file. The test cases clubbed to that group will only be executed, and the rest will be skipped

@Test(groups=”Smoke Suite”)


While the above ones should help you get started, other major annotations are:

·     @BeforeMethod and @AfterMethod – These annotations run before and after each test method

·     @BeforeClass and @AfterClass – These annotations run once before and after the first @test method in a class

·     @BeforeTest and @AfterTest – The BeforeTest annotation runs before the @BeforeClass annotation and the AfterTest annotation runs after the @AfterClass annotation

·     @BeforeSuite and @AfterSuite– These annotations run before and after any test annotated method in a class respectively. These annotations start the beginning of a test and the end of it, for all the classes in a suite

Talking about the execution order of these annotations, they execute in the below order:

@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod -> @AfterClass -> @AfterCTest -> @AfterSuite


48 views0 comments

Recent Posts

See All


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page