TestNG - Environment

TestNG is a framework for Java, so the very first requirement is to have JDK installed in your machine.
Open the console and execute a java command based on the operating system you have installed on your system.
Let's verify the output for all the operating systems −
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)
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)
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.
Set the JAVA_HOME environment variable to point to the base directory location, where Java is installed on your machine. For example,
Append Java compiler location to System Path.
Verify Java Installation using the command java -version as explained above.
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.
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.
Set the CLASSPATH environment variable to point to the TestNG jar location.
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>
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
  ===============================================


    38 Lectures 
     4.5 hours 

 Lets Kode It

    15 Lectures 
     1.5 hours 

 Quaatso Learning

    28 Lectures 
     3 hours 

 Dezlearn Education
 Print
 Add Notes
 Bookmark this page