TestNG - Environment

Advertisements


TestNG is a framework for Java, so the very first requirement is to have JDK installed in your machine.

System Requirement

JDK1.5 or above.
Memoryno minimum requirement.
Disk Spaceno minimum requirement.
Operating Systemno minimum requirement.

Step 1 - verify Java installation in your machine

Now, open console and execute the following java command.

OSTaskCommand
WindowsOpen Command Consolec:\> java -version
LinuxOpen Command Terminal$ java -version
MacOpen Terminalmachine:~ joseph$ java -version

Let's verify the output for all the operating systems:

OSOutput
Windows 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)
Linux 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)
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 installed, install the Java Software Development Kit (SDK) from http://www.oracle.com/technetwork/java/javase/downloads/index.html. We are assuming Java 1.7.0_25 as 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;

OSOutput
WindowsSet the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.7.0_25
Linuxexport JAVA_HOME=/usr/local/java-current
Macexport JAVA_HOME=/Library/Java/Home

Append Java compiler location to System Path.

OSOutput
WindowsAppend the string; C:\Program Files\Java\jdk1.7.0_25\bin to the end of the system variable, Path.
Linuxexport PATH=$PATH:$JAVA_HOME/bin/
Macnot required

Verify Java Installation using java -version command explained above.

Step 3: Download TestNG archive

Download latest version of TestNG jar file from http://www.testng.org. At the time of writing this tutorial, I downloaded testng-6.8.jar and copied it into C:\>TestNG folder.

OSArchive name
Windowstestng-6.8.jar
Linuxtestng-6.8.jar
Mactestng-6.8.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. Assuming, we've stored testng-6.8.jar in TestNG folder on various Operating Systems as follows:

OSOutput
WindowsSet the environment variable TESTNG_HOME to C:\TESTNG
Linuxexport TESTNG_HOME=/usr/local/TESTNG
Macexport TESTNG_HOME=/Library/TESTNG

Step 5: Set CLASSPATH variable

Set the CLASSPATH environment variable to point to the TestNG jar location. Assuming, we've stored testng-6.8.jar in TestNG folder on various Operating Systems as follows:

OSOutput
WindowsSet the environment variable CLASSPATH to %CLASSPATH%;%TESTNG_HOME%\testng-6.8.jar;
Linuxexport CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-6.8.jar:
Macexport CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-6.8.jar:

Step 6: Test TestNG Setup

Create a java class file name TestNGSimpleTest in C:\ > TestNG_WORKSPACE

   
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 name testng.xml in C:\ > TestNG_WORKSPACE 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:

C:\TestNG_WORKSPACE>javac TestNGSimpleTest.java

Now, invoke the testng.xml to see the result:

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

Verify the output.

===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Advertisements
Advertisements