Last Updated May 24, 2020 — Guy Arieli, CTO Quality
Continuous Testing

For this tutorial you will need:

  • Java
  • Eclipse
  • Selenium Client and WebDriver
  • TestNG

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.

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

Setup a basic TestNG project

Step 1: Install and Configure Java

TestNG requires Java which means you should download and install Java JDK5 first. You can download the Java Development Kit from the Oracle website.

Once the download is completed, double-click it and install the package.

Step 2: Configure Environmental Variables

To run Java, you should configure the environmental variable

  1. Open System Properties and click on ‘Advanced System Properties
  2. Click on Environmental variables
  3. Under user variables, check if the JAVA_HOME variable path is set to the path where Java is installed
  4. Under System variables, check if the Path value contains the java path

TestNG - Environment Variables

Step 3: Install Eclipse

Now that Java is installed, you can download and install Eclipse.

  1. To download Eclipse, visit the eclipse website.
  2. Double click the downloaded software.

    eclipse installer

  3. The next screen displays multiple options of Eclipse IDE for various tasks. Choose Eclipse IDE for Java Developers.

    Eclipse installer

  4. Accept the license agreement in the next screen.

    Eclipse agreement

  5. Check the Java folder path and installation path and Click ‘Install’.

    eclipse installer

  6. The installation process begins.

    eclipse installing

  7. After the installation is completed, launch the application.

    eclipse launch

  8. Set the workspace directory in the next screen.

    eclipse ide launcher

  9. The next screen is the welcome screen.

    eclipse ide home

Now Eclipse is installed and ready to use. We also offer our own Eclipse plugin that you can learn more about here.

Step 4: Download Selenium Client and WebDriver

To run selenium test scripts, you need to download the Selenium client and WebDriver for Java. It comes in the form of JAR files. You can download the JAR files from the Selenium website.

Step 5: Download TestNG plugin

TestNG can be easily added to Eclipse using the Marketplace option.

Click on ‘Help’ and go to Eclipse Market Place.

TestNG Eclipse Marketplace

On the next screen, type TestNG in the search box and click ‘GO’. Eclipse will display the TestNG plugin. Click on Install.

However, the latest Eclipse package is not coming with the TestNG plugin. In such a case, you can download TestNG JAR files to the system and add them as libraries to the Java project. You can download the TestNG plugin files from this link: https://dl.bintray.com/testng-team/testng-eclipse-release/

  1. Click on Zipped and choose the latest version and download the files.
  2. Now open Eclipse and click on Help and go to Install Software.
  3. Click on Add and provide the Name as TestNG Eclipse and the path where you have stored TestNG files.

    TestNG available

  4. Now click on next and accept the license agreement and install the plugin.

    testng eclpise licenses

  5. The System will prompt for a restart.

    TestNG REstart

  6. After Eclipse is restarted, click on File-> New-> Java Project.

    eclipse testng project

  7. On the next screen, enter the name of your project.

    new java project

  8. The next screen will display Java Settings. Go to Libraries and click on Add Library.

    new java project

  9. Select TestNG and click next.

    add library

  10. Click Finish and TestNG is now ready for use.

    finish

Step 6: Create a TestNG File

  1. Open Eclipse and go to Package Explorer view. Select the test project you named earlier (williamtestng here)
  2. Right-click on src and choose new ->other

    eclipse new other

  3. Now click on TestNG and it will display TestNG Class.

    TestNG class

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

    new test TestNG Eclipse

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

    TestNG Eclipse

  6. Provide the source folder and package information and choose annotations. eg: @BeforeTest and @AfterTest and click finish.
  7. Eclipse will now create your TestNG template automatically.

    TestNG Eclipse

Are you ready to scale your enterprise?

Explore

What's New In The World of Digital.ai

April 22, 2024

The Bias in the Machine: Training Data Biases and Their Impact on AI Code Assistants’ Generated Code

Explore biases in AI training data impacting code generation and learn strategies to mitigate them for fairer AI development and software innovation.

Learn More
February 22, 2024

How Futurism is Shaping Cloud Testing: A Forecast

Unlock the future of cloud testing: strategic approaches to leverage technology effectively, enhance software quality, and ensure business success.

Learn More
December 4, 2023

The Drive for Quality: Continuous Automated Software Testing for the Automotive Industry

From AI-powered test creation to self-healing systems, discover how continuous testing and innovative developments are shaping the future of connected, safe, and reliable vehicles.

Learn More