TestNG Reporting in HTML Format

In this blospost, we will learn how to generate HTML Reports for the execution of TestNG Suite.

As we already know that, JUnit does not have inbuilt Reporting Capabilities, but TestNG has a powerful way of generating HTML Reports of our test execution.

Let us go through the working example of executing a TestNG Suite and generating HTML report of the execution.

Step 1: Create one TestNG class file, and name it as TestClass1. java.

package com;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestClass1 {

@BeforeSuite
public void beforeSuite() {
System.out.println(“Inside Before Suite Method.”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“Inside Before Class of TestClass1.”);
}

@BeforeTest
public void beforeTest() {
System.out.println(“Inside Before Test Method of TestClass1.”);
}

@BeforeMethod
public void beforeMethod1() {
System.out.println(“Inside Before Method 1 of TestClass1.”);
}

@BeforeMethod
public void beforeMethod2() {
System.out.println(“Inside Before Method 2 of TestClass1.”);
}

@Test
public void testMethod1() {
System.out.println(“Inside Test Method 1 of TestClass1.”);
}

@Test
public void testMethod2() {
System.out.println(“Inside Test Method 2 of TestClass1.”);
}

@AfterTest
public void afterTest() {
System.out.println(“Inside After Test Method of TestClass1.”);
}

@AfterMethod
public void afterMethod1() {
System.out.println(“Inside After Method 1 of TestClass1.”);
}

@AfterMethod
public void afterMethod2() {
System.out.println(“Inside After Method 2 of TestClass1.”);
}

@AfterClass
public void afterClass() {
System.out.println(“Inside After Class Method of TestClass1.”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“Inside After Suite Method.”);
}
}

Step 2: Create a TestNG.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>
<test name=”TestSuiteDemo”>
<classes>
<class name=”com.TestClass1″ />
</classes>
</test>
</suite>

Step 3. Run the TestNG suite, Right Click on TestNG.xml -> Run As -> TestNG Suite. When the execution is completed, a new folder with the name test-output will be created under the project.

testng_reporting-html.PNG

Open index.html, in your preferred web browser, and you will be able to see the detailed HTML Report of TestNG suite execution.

html_reports_in_testng.PNG

That is it from this blogpost, hope it works for you!

Cheers 🙂

Advertisements