What is TestNG?

This lesson provides a brief introduction to getting started with TestNG.

We'll cover the following

Introduction #

TestNG is an easy-to-use testing framework written in Java, designed for unit, functional, and end-to-end integration tests providing many functionalities. Many plugins are integrated with TestNG.

Writing tests with TestNG is very simple. Write the business logic inside a method. Then annotate the method with @Test. Add metadata about the test inside @Test like description, group, priority, enabled, etc. This process will be covered in the next lesson.

About testng.xml #

testng.xml is a file that contains the test configuration, and it allows us to organize test classes and define test suites and tests.

Please find a sample TestNG class below:

package com.example.tests;

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

public class TestClass {

	@Test // is an annotation to indicate this is a test method
	public void testA() {
		Assert.assertTrue(true); 
	}

	@Test
	public void testB() {
		Assert.fail();
	}
}

To define this class in a testng.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample Test Suite">

    <test name="Sample Test">
        <classes>
            <class name="com.example.tests.TestClass">
            </class>
        </classes>
    </test>
</suite>

Not everything is mandatory. The bare minimum is having a suite tag with at least one test tag, containing at least one packages or a classes tag as shown in the above XML.

To know more, please follow the link.


That is all for the introduction. In the next lesson, you will study TestNG annotations.