How To Install TestNG?
System requirements for TestNG
As per TestNG site below are the system requirements:
- JDK- 1.7 or higher.
- There is no restriction on Memory, Disk space or Operating system.
How to Download TestNG?
TestNg is available in different forms. This page tells about different ways to download a TestNG. However, I suggest always to download from MVN Repository.
Downloading TestNG can be performed in the following manners too:
- Using an Eclipse Market place
- Using the command line
- Using IntelliJ plugins
- Using ANT
- Using Maven
What is TestNG Maven dependency?
On the Maven repository, TestNg libraries are also kept. We can configure and define the maven dependency to include TestNG into our project.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
How to download TestNG in Eclipse as plugins?
- Step-1 Create a java project in Eclipse.
- Step-2 Click on Help | Select Eclipse Market place from the eclipse drop-down menu.
How to open Market place in Eclipse - In the find textbox, we need to put TestNG and search
- Once we find TestNG we need to install
- Make sure the checkbox next to the URL is checked and click Next.
- The eclipse will then guide you through the process.
How to use drag and drop TestNG in Eclipse?
We need to open TestNG for Eclipse. Then drag and drop the TestNG to Eclipse.
How to install TestNG via new software?
- We need to select Help / Install New Software…
Install New Software for TestNG - Then we need to enter the update site URL in “Work with:” field: We will need to give TestNG link and click on Add button.
- and we need to provide the site for release: http://dl.bintray.com/testng-team/testng-eclipse-release/.
- We need to provide a name as TestNG and we need to click on the Add button.
- The fetching bar will show the fetching operation.
- We need to check the TestNG checkbox.
- Make sure the checkbox next to the URL is checked and click Next.
- The eclipse will then guide you through the process. Like License agreement checkbox etc.
For all the cases the Eclipse will restart to take effect.
How to Check if TestNG is properly installed in Eclipse?
To check if TestNG is properly installed in Eclipse, we need to navigate to Windows | Preferences | Select and Expand TestNG
How to configure TestNG?
This is applicable if we have downloaded the TestNG.jar. Now before we proceed further, check path and classpath settings here. Initially, we need to verify if we have java installed in our machine.
Once we are satisfied, the similar way we need to set up the TestNG into path and classpath.
Operating System | How to set |
---|---|
Windows | Set the environment variable TestNG_HOME to TestNG’s download directory |
Linux | Export TESTNG_HOME=/usr/local/TestNG |
Mac | Export TESTNG_HOME=/Library/TestNG |
Setup classpath for TestNG:
Operating System | How to set |
---|---|
Windows | Set the environment variable CLASSPATH to %CLASSPATH%;%TESTNG_HOME%\testng-X.X.jar. |
Linux | Export CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-x.x.jar. |
Mac | Export CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-x.x.jar |
TestNG Structure
Configure TestNG with Eclipse
In Eclipse, we can configure TestNG in two different ways. They are as follows:
- Adding the TestNG jar to the Build path
- Using plugins available at the market place.
Adding the TestNG jar to the Build path
We need to create a java project in Eclipse. We will follow the below steps:
- Go to File | New | Other. A window with multiple options will be shown as follows
- Now enter a Project name for a Java project, let’s say TestProject, as shown in the following screenshot, and click on Finish:
- Go to Project | right-click and select Properties. Java Build Path on the left-hand side will be auto-selected.
- Select the module and click on Add External Library option.
- We need to select TestNg.jar we have downloaded previously.
Adding the TestNG via Library
Adding the TestNG jar to the Build path
We need to create a java project in Eclipse. We will follow the below steps:
- Go to File | New | Other. A window with multiple options will be shown as follows
- Now enter a Project name for a Java project, let’s say TestProject, as shown in the following screenshot, and click on Finish:
- Go to Project | right-click and select Properties. Java Build Path on the left-hand side will be auto-selected.
- Click on the Libraries tab and click on the Add Library
- Click on Next | Finish | Apply | Apply and Close
Test the TestNG setup
We need to write a simple test if TestNG is running successfully into our system.
How to write TestNG test cases?
TestNG test creation is a four-step easy and clean process. They are as follows:
- Write a test case that expresses the business logic that needs to be validated.
- Insert TestNG notations in the code.
- Add the necessary details of your test and configurations to the testng.xml
- Execute the TestNG test.
How to write the first Test in Eclipse in TestNG?
- We need to create a java project in Eclipse.
- Add TestNG library or TestNG jar to our test.
- Create a new package say com.com.test.
- Now we need to right-click on the package we have just created.
- We need to select New | Others | then we have to expand the TestNG to select TestNG Class.
- We need to provide the details like below:
- Source folder- We need to browse the folder structure via the Browse button and select the path to the src of the project.
- Package name- we need to provide the package name. (It will mostly auto-populated).
- Class name- We need to provide the class name.
- Now we need to select the Annotations based on our requirements. They can be configured later as well.
- Lastly, we can provide an optional XML suite as well.
- Click on the Finish button.
The sample class file looks like below
package com.com.test;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class NewTest {
@Test(dataProvider = "dp")
public void f(Integer n, String s) {
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
@DataProvider
public Object[][] dp() {
return new Object[][] {
new Object[] { 1, "a" },
new Object[] { 2, "b" },
};
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}
@AfterClass
public void afterClass() {
System.out.println("After Class");
}
@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
@AfterTest
public void afterTest() {
System.out.println("After Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("After suite");
}
}