Use this step-by-step guide to set up TestNG on Eclipse
The article will help you set up a basic TestNG project using Eclipse while also giving you a great overview of TestNG and its features.
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 here:
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
- Open System Properties and click on ‘Advanced System Properties
- Click on Environmental variables
- Under user variables, check if the JAVA_HOME variable path is set to the path where Java is installed
- Under System variables, check if the Path value contains the java path
Step 3: Install Eclipse
Now that Java is installed, you can download and install Eclipse.
- To download Eclipse, visit this link: https://www.eclipse.org/downloads/
- Double click the downloaded software.
- The next screen displays multiple options of Eclipse IDE for various tasks. Choose Eclipse IDE for Java Developers.
- Accept the license agreement in the next screen.
- Check the Java folder path and installation path and Click ‘Install'.
- The installation process begins.
- After the installation is completed, Launch the application.
- Set the workspace directory in the next screen.
- The next screen is the welcome screen.
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 this link and store them in a folder:
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.
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/
- Click on Zipped and choose the latest version and download the files.
- Now open Eclipse and click on Help and go to Install Software.
- Click on Add and provide the Name as TestNG Eclipse and the path where you have stored TestNG files.
- Now click on next and accept the license agreement and install the plugin.
- The System will prompt for a restart.
- After Eclipse is restarted, click on File-> New-> Java Project.
- On the next screen, enter the name of your project.
- The next screen will display Java Settings. Go to Libraries and click on Add Library.
- Select TestNG and click next.
- Click Finish and TestNG is now ready for use.
Step 6: Create a TestNG File
- Open Eclipse and go to Package Explorer view. Select the test project you named earlier (williamtestng here)
- Right-click on src and choose new ->other
- Now click on TestNG and it will display TestNG Class.
- On the next screen, it will prompt you to enter new TestNG class details.
- For the source folder, click on the browser and choose the TestNG project.
- Provide the source folder and package information and choose annotations. eg: @BeforeTest and @AfterTest and click finish.
- Eclipse will now create your TestNG template automatically.
If you are interested in learning more about Digital.ai Continuous Testing click here.