TestNG Basics

Unit tests are written to ensure that small units of code function as intended. When it comes to Unit tests, the higher your code coverage, the higher your confidence when refactoring and/or extending code functions and features. Unit tests play an incredibly important role when it comes to sustaining and maintaining code.

TestNG is a simple, yet powerful unit test framework for Java. TestNG enables you to construct test cases quickly and assert expected results. TestNG also provides a nice alternative to JUnit and provides additional functionality including finer grained control at the test level and the ability to run tests or test classes in parallel using a pool of threads.

To illustrate its, simplicity, here is a sample TestNG unit test:

[code language=”Java”]
package com.cdimasci.test;

// statically import TestNG assertion methods
import static org.testng.Assert.fail;
import static org.testng.Assert.assertEquals;

import org.testng.AfterMethod;
import org.testng.AfterClass;
import org.testng.BeforeMethod;
import org.testng.BeforeClass;
import org.testng.Test;

public class MyNgTest {

public void setUpBeforeClass() throws Exception {
// Run once before any method in this class.

public void setUpTest() throws Exception {
// Runs once before each method annotated with @Test

public void testSomething() {
// The Sample Test case
fail("Not yet implemented");

public void testAnotherThing() {
// Another Sample Test case
Me me = new Me();
assertEquals("Carmine", me.getFirstName());

public void tearDownTest() throws Exception {
// Runs once after each method annotated with @Test.

public void tearDownAfterClass() throws Exception {
// Run once after all test cases are run


Typical uses for the above annotations

  • @BeforeClass – Typically used for for expensive test setup. Is run once before any test case is run
  • @BeforeMethod – Typically used when test cases require similar resources. We can write the code once, yet it will run before each and every test method in the test class.
  • @Test – Your test case
  • @AfterMethod – Commonly used to clean up resources that were allocated in the @BeforeMethod method. Run after each and every test case. It is guaranteed to run, even if exceptions occurs in @BeforeMethod or @Test
  • @AfterClass – Commonly used to clean up test resources allocated in @BeforeClass. This is run after all test cases run. It is guaranteed to run even if exceptions are thrown in @BeforeClass

Test Suites
Test Suites group a set of test classes. Test Suites are defined in xml.
e.g. testng.xml

[code language=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySuite">
<test name="MyTest">
<class name="com.cdimasci.test.MyNgTest" />
<test name="MyTestTwo">
<class name="com.cdimasci.test.MyNgTest2" />

Above, we have a suite containing two tests.
To run the suite, compile the java classes and run:

C:\TestNG_WORKSPACE>java -cp "c:\path\to\my\test\ng\classes" org.testng.TestNG testng.xml

Parallelism using Suites and Tests
TestNG enables multiple suites or tests to be run in parallel.

To run multiple suites in parallel, simply create 3 suites (via xml) and allocate a thread pool.

java org.testng.TestNG -suitethreadpoolsize 3 suite1.xml suite2.xml suite3.xml

Suites can also be used to create parallelism within a suite of test

TestNG provides a lot of granularity when running Tests in parallel. To take advantage of this, one can use the parallel attribute on the suites tag.

[code language=”xml”]
<suite name="My suite" parallel="methods|tests|classes|instances" thread-count="5">

  • parallel="methods": All test methods run in separate threads.
  • parallel="tests": All test methods in the same tag run in the same thread, but each tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same thread.
  • parallel="classes": All test methods in the same class run in the same thread, but each class will be run in a separate thread.
  • parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

There are a number of ways of running TestNG tests and test suites. Above we saw how to run them using the java command line. Below is an example of using the Eclipse TetsNG plugin to run our sample TestNG test above. Notice that the failure message is displayed by TestNG.



You may also like...