- TestNG Tutorial
- TestNG - Home
- TestNG - Overview
- TestNG - Environment
- TestNG - Writing Tests
- TestNG - Basic Annotations
- TestNG - Execution Procedure
- TestNG - Executing Tests
- TestNG - Suite Test
- TestNG - Ignore a Test
- TestNG - Group Test
- TestNG - Exception Test
- TestNG - Dependency Test
- TestNG - Parameterized Test
- TestNG - Run JUnit Tests
- TestNG - Test Results
- TestNG - Annotation Transformers
- TestNG - Asserts
- TestNG - Parallel Execution
- TestNG - Plug with ANT
- TestNG - Plug with Eclipse
- TestNG - TestNG - vs JUnit
- TestNG Useful Resources
- TestNG - Quick Guide
- TestNG - Useful Resources
- TestNG - Discussion
TestNG - HTML and XML Reports
TestNG comes with certain predefined listeners as part of the library. These listeners are by default added to any test execution and generate different HTML and XML reports for any test execution. The report is generated by default under the folder named testoutput and can be changed to any other folder by configuring it. These reports consist of certain HTML and XML reports that are TestNG specific.
Create Test Case Class
Create a java class, say, SampleTest.java in /work/testng/src.
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 out of which testMethodOne and testMethodThree will pass when executed, whereas testMethodTwo is made to fail by passing a false Boolean value to the Assert.assertTrue method, which is used for truth conditions in the tests.
Create testng.xml
Create testng.xml in /work/testng/src to execute test case(s).
<?xml version = "1.0" encoding = "UTF-8"?> <suite name = "Simple HTML-XML Suite"> <test name = "Simple HTML-XML test"> <classes> <class name = "SampleTest" /> </classes> </test> </suite>
Compile the SampleTest class using javac.
/work/testng/src$ javac SampleTest.java
Now, run the testng.xml.
/work/testng/src$ java org.testng.TestNG testng.xml
Verify the output.
=============================================== Simple HTML-XML Suite Total tests run: 3, Failures: 1, Skips: 1 ===============================================
Now, go to the /work/testng/src/test-output folder. Open the index.html on your default web browser. You will see the following HTML report −
Now, open the file /work/testng/src$/test-output/testing-results.xml in the default XML editor on your system, and you will see the following content in the XML file −
TestNG, by default, generates multiple reports as part of its test execution. These reports mainly include TestNG HTML report, TestNG email-able report, TestNG report XML, and JUnit report XML files. These files can be found under the output report folder (in this case, test-output).
This default report generation can be disabled while running the tests by setting the value of the property useDefaultListeners to false. This property can be set while using build tools like Ant or Maven.