TestNG Hello World Example

In this post we will learn a very basic Hello World example using TestNG. We will learn how to setup environment to use testNG and how to actually write & execute unit tests and verify results. We will be using maven based project. Let’s begin.


Following environment being used:

  • TestNG 6.9.4
  • Maven 3
  • JDK 1.7
  • Eclipse JUNO Service Release 2

Project directory structure:

Following will be the maven based project directory structure for this example:
TestNGHelloWorld_img1

Let’s now add the content mentioned in above structure explaining each in detail.

Step 1: Add Dependency in pom.xml

Add TestNG dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.websystique.testng</groupId>
	<artifactId>TestNGHelloWorldExample</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>TestNGHelloWorldExample</name>

	<dependencies>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.9.4</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

Note the scope element set to test, as we are using testNG only for test purpose.

Step 2: Create Sample Java Class

Below is a sample Java class whose methods we will be testing in this post. Our class contains only one method which we will unit test.

package com.websystique.testng;

public class VatCalculator {
	
	/*
	 * Returns 21% VAT on given amount
	 */
	public double getVatOnAmount(double amount){
		return amount * 0.21;
	}

}

Step 3: Create Test class

Below is a Test class used to unit-test the method getVatOnAmount from above Java class VatCalculator.

package com.websystique.testng;

import org.testng.annotations.Test;
import org.testng.Assert;

public class TestVatCalculator {

	@Test
	public void testGetVatOnAmount(){
		VatCalculator calc = new VatCalculator();
		double expected = 21;
		Assert.assertEquals(calc.getVatOnAmount(100), expected);
		Assert.assertNotEquals(calc.getVatOnAmount(120), expected);
	}
}

@Test annotation is what makes a method a test method. With TestNG, unit-testing a method involves calling that actual method in a test method annotated with @Test, asserting the output from actual method for success/failure. That’s what we did above. We called the getVatOnAmount on a real instance and then used TestNG Assert api to assert the return value from actual method. If any of the assertion fails, complete @Test will fail. Assert API provides dozens of method for comparing the expected result with the actual result.

Important points:
Source files are created under src/main/java and test files are created under src/test/java. It’s best practice to create the test files in a package created with same name as the package containing actual source files. Mimicing the same package name in test as in source keep things clean and understandable.

Name of test class must have ‘Test’ as prefix or suffix if you plan to execute the tests via maven (mvn test). This does not hold if you are running the test via eclipse plugin. Also, the Test class name should ideally reflect the source class name you are testing.

Name of test method can be anything. Ideally, name should reflect which method of source file you are testing. Including ‘test’ is not required in testcase name.

Step 4: Execute Tests

Tests can be executed in many different ways, some are…

  • Using TestNG Eclipse plugin
  • Using maven [ mvn test OR mvn install]

Using TestNG Eclipse plugin:
Install the plugin using Eclipse market place(search for testNG) or as mentioned on plugin page.
Once plugin installed successfully, right click on the test class and run as “TestNG Test”.

You will see following output:

TestNGHelloWorld_img2

Additionally, you will see that a folder named ‘test-output’ is now added to your project which contains different files related to results of unit test you preformed (specially testng-results.xml).

Using Maven :

Just browse through project using command line, perform mvn clean test. Maven uses maven-surefire-plugin to execute the tests.

E:\workspace7\TestNGHelloWorldExample>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestNGHelloWorldExample 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGHelloWorldExample ---
[INFO] Deleting E:\workspace7\TestNGHelloWorldExample\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGHelloWorldExample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d
ependent!
[INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ TestNGHelloWorldExample ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende
nt!
[INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGHelloWorldExample --
-
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d
ependent!
[INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ TestNGHelloWorldExample ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende
nt!
[INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TestNGHelloWorldExample ---
[INFO] Surefire report directory: E:\workspace7\TestNGHelloWorldExample\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.websystique.testng.TestVatCalculator
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@3a4346cd
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.976 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.150s
[INFO] Finished at: Fri Jul 03 20:26:37 CEST 2015
[INFO] Final Memory: 12M/226M
[INFO] ------------------------------------------------------------------------
E:\workspace7\TestNGHelloWorldExample>

You can see the report of executed tests above. Total test run were 1, out of which none has failed, no error, none skipped. Note that had tests been failed, the build would have failed. After the run, you will find a folder named ‘surefire-reports’ being added to your target folder, which contains unit-test result reports(specially testng-results.xml).

Note that maven test goal is anyway executed when you perform mvn clean install, so you may prefer to use mvn clean install instead of performing mvn clean test in order to execute unit test and install in one shot.

That’s it. As we mentioned before, it was a basic introduction to testNG setup. We will go through the testNG features in detail in subsequent posts.

Next post introduces you to commonly used TestNG annotations and how you can configure your test setup using them.

References