Labels: Io using Java for file sp html amp on
In this section, we will introduce an example of writing a user-defined recorder and testng method. To compile a custom recorder class, our extension class should implement the ireporter interface. Let's move on and create an example using a custom recorder.
Create test case class
Create a Java class named sampletest. Java in c: \> testng_workspace
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); }}
The preceding test class contains three test methods. testmethodone and testmethodthree are executed, while testmethodtwo is assert by a falseboolean value. asserttrue method, which is used to test the True Value Condition failure.
Create custom Report class
Create another class named customreporter. Java in c: \> testng_workspace
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<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 previous class implements the org. testng. ireporter interface. It implements the generatereport method defined by the ireporter interface. This method has three parameters:
The first is xmlsuite, which is the list suite mentioned in the execution of testng test XML.
The second is the suite, which contains a set of information after test execution. The object contains all information packages, classes, test methods, and test execution results.
Outputdirectory, the Output Folder path generated in the report, including the information.
Create testng. xml
Create a file testng. XML in c: \> testng_workspace to execute the test case
<?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>
Compile sampletest and use javac for the customreporter class
C:\TestNG_WORKSPACE>javac CustomReporter.java SampleTest.java
Run testng. xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify output
===============================================Simple Reporter SuiteTotal tests run: 3, Failures: 1, Skips: 1===============================================Passed tests for suite ‘Simple Reporter Suite‘ is:1Failed tests for suite ‘Simple Reporter Suite‘ is:1Skipped tests for suite ‘Simple Reporter Suite‘ is:1
The preceding example shows a simple user-defined reporter. The number of printed items on the console fails to be included in the preceding test for each suite. The test is skipped. The reporter is mainly used for testing execution to generate the final report. Extensions can be used to generate xml, HTML, CHM, CSV, or text files according to the Report requirements.
[Test Report]-testng custom Recorder