top of page

Introduction to TestNG

TestNG in Selenium :

Automation testing is a fast-growing industry and every tester tends to opt for tools and frameworks that are self-sufficient and offer useful features out of the box. Though there are number of test automation frameworks like Selenium, Cypress, etc. I still prefer using Selenium.

The reason for choosing Selenium is very simple – Selenium supports the Java programming language.

The features offered by Selenium multiply when it is used in conjunction with Java-based test automation frameworks like TestNG, JUnit, etc.

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. A few advanced and useful features provided by TestNG make it a more robust framework compared to its peers. The NG in TestNG stands for ‘Next Generation.’ 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.

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 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 pretty easy to install TestNG. If you are using Eclipse IDE, it comes as a plugin to it. 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.

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

3. Click on 'Eclipse Marketplace' you will be directed to the marketplace modal. Type TestNG in the Keyword and hit 'Go' The referenced snapshot is below:

4.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. As a good practice, Eclipse would recommend you to restart to use the features of the installed plugin.

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

TestNG Annotations

An annotation is a tag that 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:

@Test– This is the root of TestNG test cases. In order to use TestNG, all methods should be annotated with this annotation. Below is an example:

@Test public void setupTestNG() { System.out.println(“this is a test annotation method”) }

A few attributes associated with the TestNG annotation are:

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

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

2. 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.


3. DependsOnMethod: This attribute works miracles if one test case is dependent on the other test case. 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


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


5. Groups: Using this attribute, you can club your test cases into a single group and specify the specified 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

TestNG Assertions

Like JUnit, TestNG provides multiple level assertions to validate your actual results against your expected results. Few of the commonly used assertions are:

1. assertTrue– This assertion verifies whether the defined condition is true or not. If true, it will pass the test case. If not, it will fail the test case


2. assert False– This assertion verifies whether the defined condition is false or not. If false, it will pass the test case. If not, it will fail the test case


3. assert Equals– This assertion compares the expected value with the actual value. If both are the same, it passes the test case. If not, it fails the test case. You can compare strings, objects, integer values etc. using this assert


4. assertNotEquals: This is just opposite to what assert Equals does. If actual matches the expected, the test case fails, else the test case passes


An important part to note in assertions is that your tests will not execute to the next line of code if your assertions failed. It will automatically jump to the next test annotated method.

Now, let us try to validate our code snippet below using assertions. In the snippet above, in the @AfterClass method, we will be verifying the current URL we are on and the expected URL, which should be the signup page.

@AfterMethod public void postSignUp() { Assert.assertEquals(driver.getCurrentUrl(); }

Make this change in your code snippet above and execute it. The test case should pass. Also try giving another expected URL, to validate if the tests fail.

Advantages of using TestNG over other frameworks

TestNG provides multiple powerful features compared to other test automation frameworks. This can be explained better by seeing the advantages of TestNG over another popular framework, JUnit.

1. Annotations of TestNG are easier to understand compared to JUnit

2. TestNG does not require you to mandatorily declare @BeforeClass and @AfterClass, compared to JUnit

3.The feature of parametrization provided by TestNG is more convenient and easier to use through dataprovider

4. Features like prioritization and grouping of tests provided by TestNG make it more realistic and adaptable as compared to JUnit

5. TestNG allows dependency on multiple methods, making your tests more maintainable and flow-specific as compared to JUnit

6.TestNG provides the facility of parallel execution in multiple ways compared to JUnit

7. TestNG gives default reporting in HTML and XML format. These can further be customized using multiple listeners. This makes it much more friendly as compared to JUnit

Having said that, JUnit also has certain advantages compared to TestNG, which we will cover separately. Do note that BrowserStack supports both running Selenium tests using TestNG and running Selenium tests using JUnit.

As an engineering function, if you are automating your test cases and are thinking about choosing the right framework, start by first listing down your current needs and usage. Also, think about what you will need as you scale. Then decide on the framework that works best for you. Avoid picking the latest framework in the market, since that might hamper you from scaling down the line.

Key Takeaways

TestNG makes automated tests more structured, readable, maintainable, and user-friendly. It provides powerful features and reporting. Its high-end annotations like data provider, makes it easier to scale up, as you perform cross browser testing across multiple devices, browsers, and their versions. Automated Selenium testing tools like BrowserStack supports frameworks like TestNG to write and execute test cases.

179 views0 comments

Recent Posts

See All

Exception Handling in Selenium Webdriver

What is an exception? An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prev


Mit 0 von 5 Sternen bewertet.
Noch keine Ratings

Rating hinzufügen
bottom of page