”;
TestNG is a framework for Java, so the very first requirement is to have JDK installed in your machine.
System Requirement
JDK | 1.7 or above. |
Memory | No minimum requirement. |
Disk Space | No minimum requirement. |
Operating System | No minimum requirement. |
Step 1 – Verify Java Installation in Your Machine
Open the console and execute a java command based on the operating system you have installed on your system.
OS | Task | Command |
---|---|---|
Windows | Open Command Console | c:> java -version |
Linux | Open Command Terminal | $ java -version |
Mac | Open Terminal | machine:~ joseph$ java -version |
Let”s verify the output for all the operating systems −
OS | Output |
---|---|
Windows |
java version “15.0.2” 2021-01-19 Java(TM) SE Runtime Environment (build 15.0.2+7-27) Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing) |
Linux |
openjdk version “11.0.11” 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing) |
Mac |
java version “1.7.0_25” Java(TM) SE Runtime Environment (build 1.7.0_25-b15) Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode) |
If you do not have Java, install the Java Software Development Kit (SDK) from https://www.oracle.com/technetwork/java/javase/downloads/index.html. We are assuming Java 1.7.0_25 as the installed version for this tutorial.
Step 2: Set JAVA Environment
Set the JAVA_HOME environment variable to point to the base directory location, where Java is installed on your machine. For example,
OS | Output |
---|---|
Windows | Set the environment variable JAVA_HOME to C:Program FilesJavajdk15.0.2. |
Linux | export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java. You can also set this environment variable in the /etc/environment. Restart the machine after setting else it will be lost in the session. |
Mac | export JAVA_HOME=/Library/Java/Home. |
Append Java compiler location to System Path.
OS | Output |
---|---|
Windows | Append the string C:Program FilesJavajdk1.7.0_25bin at the end of the system variable, Path. |
Linux | export PATH=$PATH:$JAVA_HOME/bin/ |
Mac | Not required |
Verify Java Installation using the command java -version as explained above.
Step 3: Download TestNG Archive
Download the latest version of TestNG jar file from http://www.testng.org or from here. At the time of writing this tutorial, we have downloaded testng-7.4.jar and copied it onto /work/testng folder.
OS | Archive name |
---|---|
Windows | testng-7.4.jar |
Linux | testng-7.4.jar |
Mac | testng-7.4.jar |
Step 4: Set TestNG Environment
Set the TESTNG_HOME environment variable to point to the base directory location, where TestNG jar is stored on your machine. The following table shows how to set the environment variable in Windows, Linux, and Mac, assuming that we”ve stored testng-7.4.jar at the location /work/testng.
OS | Description |
---|---|
Windows | Set the environment variable TESTNG_HOME to C:testng. |
Linux | export TESTNG_HOME=/work/testng. You can also set this environment variable in the /etc/environment. Restart the machine after setting else it will be lost in the session. |
Mac | export TESTNG_HOME=/Library/testng |
Step 5: Set CLASSPATH Variable
Set the CLASSPATH environment variable to point to the TestNG jar location.
OS | Description |
---|---|
Windows | Set the environment variable CLASSPATH to %CLASSPATH%;%TESTNG_HOME%testng-7.4.jar. |
Linux | export CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-7.4.jar. This classpath is applicable only for current session. Once the current command termical closes, you will have to reset it. |
Mac | export CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-7.4.jar. |
Step 6: Test TestNG Setup
Create a java class file named TestNGSimpleTest at /work/testng/src
import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; public class TestNGSimpleTest { @Test public void testAdd() { String str = "TestNG is working fine"; AssertEquals("TestNG is working fine", str); } }
TestNG can be invoked in several different ways −
- With a testng.xml file.
- With ANT.
- From the command line.
Let us invoke using the testng.xml file. Create an xml file with the name testng.xml in /work/testng/src to execute Test case(s).
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "TestNGSimpleTest"/> </classes> </test> </suite>
Step 7: Verify the Result
Compile the class using javac compiler as follows −
/work/testng/src$ javac TestNGSimpleTest.java
Now, invoke the testng.xml to see the result −
/work/testng/src$ java org.testng.TestNG testng.xml
Verify the output.
=============================================== Suite Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 ===============================================
”;