How to generate reports in TestNG

In this post, we will learn how to generate test reports in TestNG. We will be using ReportNG to make reports, as it generates better reports than the default TestNG reports.

We will follow some simple steps to generate reports.

Add ReportNG dependencies

<!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
<dependency>
    <groupId>org.uncommons</groupId>
    <artifactId>reportng</artifactId>
    <version>1.1.4</version>
</dependency>

Create a Test Class

package Test;

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

public class CodekruTest {

	@Test
	public void test() {
		int a = 5;
		int b = 6;
		int c = 11;
		Assert.assertTrue((a + b) == c);
	}

}

Add Reporter log

Now, add Reporter.log(comment) with the comment you want to see in the report. We have also placed a log, as shown below.

package Test;

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

public class CodekruTest {

	@Test
	public void test() {
		int a = 5;
		int b = 6;
		int c = 11;
		Reporter.log("Executing the test"); // this will appear in our report
		Assert.assertTrue((a + b) == c);
	}

}

Disable the default TestNG Listeners

  • Right-click on your project and select Properties.
  • Go to the TestNG.
  • Tick the checkbox of Disable default listeners.
  • Then click on Apply and Close.
Disable default listeners

Make testng.xml file

We have to add the below listeners in our testng.xml file

    org.uncommons.reportng.HTMLReporter
    org.uncommons.reportng.JUnitXMLReporter

So, our testng.xml file will look like below

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="codekru">
	<listeners>
		<listener class-name="org.uncommons.reportng.HTMLReporter" />
		<listener
			class-name="org.uncommons.reportng.JUnitXMLReporter" />
	</listeners>
	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTest" />
		</classes>
	</test>
</suite>

Now, run the testng.xml file, and let’s see whether a report is generated or not 😛

The thing is, you will get an error –

java.lang.NoClassDefFoundError: com/google/inject/Injector
	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3325)
	at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3350)
	at java.base/java.lang.Class.getMethods(Class.java:1996)
	at org.apache.velocity.util.introspection.ClassMap.getAccessibleMethods(ClassMap.java:258)
	at org.apache.velocity.util.introspection.ClassMap.populateMethodCache(ClassMap.java:151)
	at org.apache.velocity.util.introspection.ClassMap.<init>(ClassMap.java:64)
	at org.apache.velocity.util.introspection.IntrospectorBase.createClassMap(IntrospectorBase.java:126)
	at org.apache.velocity.util.introspection.IntrospectorBase.getMethod(IntrospectorBase.java:112)
	at org.apache.velocity.util.introspection.Introspector.getMethod(Introspector.java:100)
	at org.apache.velocity.runtime.parser.node.PropertyExecutor.discover(PropertyExecutor.java:65)
	at org.apache.velocity.runtime.parser.node.PropertyExecutor.<init>(PropertyExecutor.java:39)
	at org.apache.velocity.util.introspection.UberspectImpl.getPropertyGet(UberspectImpl.java:156)
	at org.apache.velocity.runtime.parser.node.ASTIdentifier.execute(ASTIdentifier.java:125)
	at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:175)
	at org.apache.velocity.runtime.parser.node.ASTReference.evaluate(ASTReference.java:307)
	at org.apache.velocity.runtime.parser.node.ASTNotNode.evaluate(ASTNotNode.java:45)
	at org.apache.velocity.runtime.parser.node.ASTExpression.evaluate(ASTExpression.java:45)
	at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:68)
	at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:55)
	at org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:166)
	at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:114)
	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:230)

So, we have to add one more dependency in our pom.xml

		<dependency>
			<groupId>com.google.inject</groupId>
			<artifactId>guice</artifactId>
			<version>4.2.0</version>
		</dependency>

Now, let’s rerun the testng.xml and see what happens 😛

Our report is generated, but where? Please refresh the project once, and you will see an index.html file under the test-output > html folder.

reportng customized report

Open that index.html file in the browser, and you will have your incredible report. 🙂

generated report

Make Colored HTML reports using ReportNG.

Do you all know that we can make colored reports using ReportNG, like

  • Passing the test in green color.
  • We are failing the test in red color.
  • Or raising the warning in orange color.

Add the below property in the code.

 System.setProperty("org.uncommons.reportng.escape-output", "false");

Add coloring using HTML code like below

String message = "<font color = 'green'>" + "Test is successfully executed" + "</font>";
Reporter.log(message);

The whole class will be like below

package Test;

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

public class CodekruTest {

	@Test
	public void test() {
		int a = 5;
		int b = 6;
		int c = 11;

		System.setProperty("org.uncommons.reportng.escape-output", "false");

		Reporter.log("Executing the test"); // this will appear in our report
		Assert.assertTrue((a + b) == c);
		String message = "<font color = 'green'>" + "Test is successfully executed" + "</font>";
		Reporter.log(message); // this will also appear in our report but in green color
	}

}

Now, run the same testng.xml file we made earlier, and you will see the Test is successfully executed in green color in the generated report.

colored report

Github link for the above tutorial – https://github.com/CodekruTeam/GenerateReportsInTestNG/tree/master.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.