Custom Reporter in TestNG
Custom reporter in testng will discuss about writing our own reporter in TestNG. Reporting is one of the most important things to remember in automation testing. Reports are used to understand the result of the test execution, point of failure, and reasons for the failure. TestNG by default generates a different type of report (i.e. HTML and an XML reports). TestNG also allows us to write their own reporter and use it with TestNG.
To generate custom reports in testng we need to use Reporters. To implement reporters, need to implement the IReporter interface in your class. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called.
For this we need to have at least 2 classes. One is for actual test cases and other is for to implement the IReporter.
Below is the sample program for test cases class:
import org.testng.Assert; import org.testng.annotations.Test; public class loggerTest { @Test public void testOne() { Assert.assertTrue(true); } @Test public void testTwo() { Assert.assertTrue(false); } @Test(dependsOnMethods = { "testTwo" }) public void testThree() { Assert.assertTrue(true); } }
When you execute the above program, the status of the test cases should be listen by the listener class. We have three methods called testOne, testTwo and testThree in the above program one is to pass and second is to fail and third is to skip status. After execution of the suits, the listener will track the status of the each test.
Below is the sample listener class which will implements the IReporter
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<XmlSuite> xmlSuites, List<ISuite> 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<String, ISuiteResult> suiteResults = suite.getResults(); for (ISuiteResult sr : suiteResults.values()) { ITestContext tc = 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()); } } } }
The above class implements the IReporter. So, it has to implement all the methods which are available in that interface. And it is not mandatory to use all the methods, we can left blank some of the methods which are not relevant to the requirement.
Now we have to create a testng.xml file to execute the above tests and it should have a mandatory tag called listener. It will call the listener class to listen the status of the each and every test which will execute.
Below is the sample program to execute the above program:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <listeners> <listener class-name="CustomReporter"></listener> </listeners> <test name="Test"> <classes> <class name="loggerTest"/> </classes> </test> </suite>
Output for the above program is:
This way we can use custom listeners to generate the reports after execution.
Please watch the you tube video for better understanding.