What is TestNG?

TestNG is an automation testing framework in which NG stands for "Next Generation". TestNG is inspired from Junit which uses the annotations (@). TestNG overcomes the disadvantages of JUnit and is designed to make end-to-end testing easy.

Using TestNG, you can generate a proper report, and you can easily come to know how many test cases are passed, failed, and skipped. You can execute the failed test cases separately.

For example:

  • Suppose you have 10 test cases, one method is written for each test case (Assume that the program is written using the main method without using testNG). When you run this program first, three methods are executed successfully, and the fourth method is failed. Then correct the errors present in the fourth method, now you want to run only fourth method because first three methods are anyway executed successfully. This is not possible without using TestNG.
  • The TestNG in Selenium provides an option, i.e., testng-failed.xml file in test-output folder. If you want to run only failed test cases means you run this XML file. It will execute only failed test cases.

Beside above concept, you will learn more on TestNG, like what are the Advantages of TestNG, how to create test methods using @test annotations, how to convert these classes into testing suite file and execute through the eclipse as well as from the command line.

Why Use TestNG with Selenium?

Default Selenium tests do not generate a proper format for the test results. Using TestNG in Selenium, we can generate test results.

Most Selenium users use this more than Junit because of its advantages. There are so many features of TestNG, but we will only focus on the most important ones that we can use in Selenium. Following are the key features of Selenium TestNG:

  • Generate the report in a proper format including a number of test cases runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
  • Multiple test cases can be grouped more easily by converting them into testng.xml file. In which you can make priorities which test case should be executed first.
  • The same test case can be executed multiple times without loops just by using keyword called 'invocation count.'
  • Using testng, you can execute multiple test cases on multiple browsers, i.e., cross browser testing.
  • The TestNG framework can be easily integrated with tools like TestNG Maven, Jenkins, etc.
  • Annotations used in the testing are very easy to understand ex: @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest
  • WebDriver has no native mechanism for generating reports. TestNG can generate the report in a readable format like the one shown below.

  • TestNG simplifies the way the tests are coded. There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.
  • Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.

Advantages of TestNG over JUnit

There are three major advantages of TestNG over JUnit:

  • Annotations are easier to understand.
  • Test cases can be grouped more easily.
  • Parallel testing is possible.

Annotations in TestNG are lines of code that can control how the method below them will be executed. They are always preceded by the @ symbol.

Example is shown below.

package tests;

import org.testng.annotations.Test;

public class TestNGDemo {
   
    @Test(priority = 0)
    public void testA(){
        System.out.println("Test A");
    }

    @Test(priority = 1)
    public void testB(){
        System.out.println("Test B");
    }
}

The ability to run tests in parallel is available in TestNG but not in JUnit, so the TestNG framework is more preferred of testers using Selenium Grid.

Create Test Case Using TestNG Annotations

Let us see how to create our first test case using TestNG Annotations in Selenium:

Before we create a test case, we should first setup a new TestNG Project in Eclipse and name it as "TestNGProject".

Step 1: Click File > New > Java Project.

Step 2: Type "TestNGProject" as the Project Name then click Next.

Step 3: We will now start to import the TestNG Libraries onto our project. Click on the "Libraries" tab, and then "Add Library…"

Step 5: Click Finish.

You should notice that TestNG is included on the Libraries list.

Step 6: We will now add the JAR files that contain the Selenium API. These files are found in the Java client driver that we downloaded from http://docs.seleniumhq.org/download/

Then, navigate to where you have placed the Selenium JAR files.

Step 7: Click Finish and verify that our TestNGProject is visible on Eclipse's Package Explorer window.

How to Create a New TestNG Test File

Now that we are done setting up our project in this TestNG tutorial, let us create a new TestNG file.

Step 1: Right-click on the "src" package folder then choose New > Other…

Step 2: Click on the TestNG folder and select the "TestNG class" option. Click Next.

Step 3: Type the values indicated below on the appropriate input boxes and click Finish. Notice that we have named our Java file as "FirstTestNG".

Eclipse should automatically create the template for our TestNG file.

Coding of our First TestNG Test Case Example

Let us now create our first Test Case that will check if Rahul Shetty Academy homepage is correct. Type your code as shown in the below TestNG Example:

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGDemo {

    @Test(priority = 0)
    public void verifyHomepage(){
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //code to maximize chrome browser
        driver.manage().window().maximize();

        String baseURL = "https://www.rahulshettyacademy.com/AutomationPractice/";
        driver.get(baseURL);

        String expectedTitle = "Practice Page";
        String actualTitle = driver.getTitle();
        Assert.assertEquals(actualTitle, expectedTitle);
        driver.close();
    }
}

Notice the following.

  • TestNG does not require you to have a main() method.
  • Methods need not be static.
  • We used the @Test annotation. @Test is used to tell that the method under it is a test case. In this case, we have set the verifyHomepage() method to be our test case, so we placed an '@Test' annotation above it.
  • Since we use annotations in TestNG, we needed to import the package org.testng.annotations.*.
  • We used the Assert class. The Assert class is used to conduct verification operations in TestNG. To use it, we need to import the org.testng.Assert package.

You may have multiple test cases (therefore, multiple @Test annotations) in a single TestNG file. This will be tackled in more detail later in the section "Annotations used in TestNG."

Running the Test

To run the test, simply run the file in Eclipse as you normally do. Eclipse will provide two outputs – one in the Console window and the other on the TestNG Results window.

Generating HTML Reports

TestNG can generate reports in HTML format.

Step 1: After running our FirstTestNG that we created in the previous section, right-click the project name (TestNGProject) in the Project Explorer window then click on the "Refresh" option.

Step 2: Notice that a "test-output" folder was created. Expand it and look for an index.html file. This HTML file is a report of the results of the most recent test run.

Step 3: Double-click on that index.html file to open it within Eclipse's built-in web browser. You can refresh this page any time after you rerun your test by simply pressing F5 just like in ordinary web browsers.

Annotations used in TestNG

In the previous section, you have been introduced to the @Test annotation. Now, we shall be studying more advanced annotations and their usages.

Multiple Test Cases

We can use multiple @Test annotations in a single TestNG file. By default, methods annotated by @Test are executed alphabetically. See the code below. Though the methods c_test, a_test, and b_test are not arranged alphabetically in the code, they will be executed as such.

package firsttestngpackage;

import org.testng.Assert;

import org.testng.SkipException;

import org.testng.annotations.Test;

public class SecondTestNG {

       @Test

       public void c_test() {

              Assert.fail();

       }

      

       @Test

       public void a_test() {

              Assert.assertTrue(true);;

       }

      

       @Test

       public void b_test() {

              throw new SkipException("Skip b_test");

       }}

Run this code and on the generated index.html page, click "Chronological view."