Reading:  

Working with TestNG - Starters Guide


Test Reports

Reporting is useful to understand failure point, reasons for failure and the test execution. Reporting is a part of execution. User can log to know the execution flow or for debugging of failure if any.

TestNG has inbuilt capability to generates a types of report for its test execution.Types of report are HTML and an XML report. Users can write their own reporter which are notified runtime by TestNG and also can write their own loggers.

Two ways of generating a report with TestNG:

  • Listeners: To implement a listener class it should implement testng.ITestListener interface. These classes are notified at execution time by TestNG when the test starts, fails, finishes, skips or passes.
  • Reporters: To implement a reporting class it should implement an testng.IReporter interface. These classes are called when the whole suite execution completes. The object containing the information of the whole test run is passed to this class when called.

Four different reporting and logging are:

Custom Logging:

Example:We are going to custom logger class, which implements the ITestListener interface and attached itself to the TestNG test suite as a listener. Methods of listener class are invoked by TestNG when test started, at test fail, at test success etc.

Create Test Case Class named as LogClass.java

package testNG;

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

public class LogClass {@Test(priority = 0)
	public void method1() {
		System.out.println("Inside the method1");
	}

	@Test(priority = 1)
	public void method2() {
		System.out.println("Inside the method2");
		Assert.assertTrue(false);
	}

	@Test(dependsOnMethods = {
		"method2"
	})
	public void method3() {
		System.out.println("Insdide method3");
	}
}

 

Create Custom Logging Class CustListner.java extends classTestListenerAdapter

package testNG;

import org.testng.IClass;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class CustListner extends TestListenerAdapter {@Override
	Public void onTestStart(ITestResultitr) {
		log("Test Started");
	}

	@Override
	public void onTestSuccess(ITestResultitr) {

		log("Test : " + itr.getName() + "PASSED");

		// This will print the class name in which the method is present
		log(itr.getTestClass());

		// Print the priority of the method,If there is no priority then print the default priority as'0'
		log("The priority of method is " + itr.getMethod().getPriority());

		System.out.println("======================");
	}

	@Override
	public void onTestFailure(ITestResultitr) {

		log("Test : " + itr.getName() + " FAILED");
		log("Priority of this method is " + itr.getMethod().getPriority());
		System.out.println("=======================");
	}

	@Override
	public void onTestSkipped(ITestResultitr) {
		log("Test : " + itr.getName() + " SKIPPED");
		System.out.println("=======================");
	}
	private void log(String nameOfMethod) {
		System.out.println(nameOfMethod);
	}

	private void log(IClasstsClass) {
		System.out.println(tsClass);
	}
}

Create testing.xml file as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
    <listeners>
        <listener class-name="testNG.CustListner" />
    </listeners>
    <test name="listner test case">
        <classes>
            <class name="testNG.LogClass"/>
            <methods>
                <include name="method1" />
                <include name="method2" />
                <include name="method3" />
            </methods>
        </classes>
    </test>
</suite>
 

Run the testing.xml file by click on run button in eclipse.

The Output is:

Custom loggers and test reports testNG

Custom Reporter

Create a Class named as SampleTest.java

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

public class SampleTest {@Test
	public void testMethodOne() {
		Assert.assertTrue(true);
	}

	@Test
	public void testMethodTwo() {
		Assert.assertTrue(false);
	}

	@Test(dependsOnMethods = {
		"testMethodTwo"
	})
	public void testMethodThree() {
		Assert.assertTrue(true);
	}
}

Create Custom Reporting Class

import java.util.List;
import java.util.Map;

import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.xml.XmlSuite;

public class CustomReporter implements IReporter {@Override
	public void generateReport(List xmlSuites, List suites,
	String outputDirectory) {
		//Iterating over each suite included in the test
		for (ISuite suite: suites) {
			//Following code gets the suite name
			String suiteName = suite.getName();
			//Getting the results for the said suite
			Map suiteResults = suite.getResults();
			for (ISuiteResultsr: suiteResults.values()) {
				ITestContexttc = sr.getTestContext();
				System.out.println("Passed tests for suite '" + suiteName +
					"' is:" + tc.getPassedTests().getAllResults().size());
				System.out.println("Failed tests for suite '" + suiteName +
					"' is:" + tc.getFailedTests().getAllResults().size());
				System.out.println("Skipped tests for suite '" + suiteName +
					"' is:" + tc.getSkippedTests().getAllResults().size());
			}
		}
	}
}

Create testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Simple Reporter Suite">
    <listeners>
        <listener class-name="CustomReporter" />
    </listeners>
    <test name="Simple Reporter test">
        <classes>
            <class name="SampleTest" />
        </classes>
    </test>
</suite>

Run the testing.xml file by click on run button in eclipse.

The Output is:

HTML and XML reports example testNG

TestNG HTML and XML Reports:

By default TestNG generates multiple reports at the tile of test execution. These reports include TestNG HTML report,TestNG report XML, and alsoJUnit report XML files. These files are created under the output report folder test-output.

Create a Class named as LogClass.java

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

public class LogClass {

	@Test(priority = 0)
	public void method1() {
		System.out.println("Inside the method1");
	}

	@Test(priority = 1)
	public void method2() {
		System.out.println("Inside the method2");
		Assert.assertTrue(false);
	}

	@Test(dependsOnMethods = {
		"method2"
	})
	public void method3() {
		System.out.println("Insdide method3");
	}
}

Create testing.xml file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
    <test name="html report">
        <classes>
            <class name ="testNG.LogClass"/>
        </classes>
    </test>
</suite>
 

Run the testing.xml file by click on run button in eclipse.

The Output is:

Go to the C:\>Users\testng\workspace\test-output folder. Double click on index.html. We will see the following HTML report:

testNG HTML report

To see the xml report double click on xml cocument in test-output folder inside the html report folder. We will see the following XML report.

XML report example testNG

 

Description

This tutorial is focused on getting you started on TestNG, the testing framework inspired from JUnit and NUnit. Here is a quick table of contents

  1. What is TestNG?
  2. Environment Set-up
  3. Writing Tests
  4. Basic Annotations
  5. Execution Procedure
  6. Executing Tests
  7. Suite Test
  8. Ignore Test
  9. Group Test
  10. Exception Test
  11. Dependency Test
  12. Parametrized Test
  13. JUnit Tests
  14. Test Reports
  15. Running tests without Eclipse
  16. Plugin with ANT

 

 



Environment

A computer capable of running Java. IntelliJ Community IDE is also required for this course.

Prerequisites

Good knowledge of Java programming language and eclipse is essential.

Audience

Students looking to get started with TestNG

Learning Objectives

This tutorial will get you started with TestNG.

Author: Subject Coach
Added on: 12th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!