Extent Reports in TestNG
Extent Reports is a reporting library for automated testing that provides detailed and customizable test reports. To use Extent Reports with TestNG, you can follow these steps:
- Add the Extent Reports library to your project: You can add the Extent Reports library to your project by adding the following dependency to your build tool (e.g. Maven or Gradle):
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.9</version>
</dependency>
2. Create an ExtentReports instance in your test
public class TestClass {
private ExtentReports extent;
@BeforeTest
public void beforeTest() {
extent = new ExtentReports();
extent.attachReporter(new ExtentHtmlReporter("extent.html"));
}
// your test methods
}
In this example, an ExtentReports instance is created in the beforeTest method and a new ExtentHtmlReporter is attached to it. The report will be saved to an HTML file named "extent.
3. Add Extent Reports reporting to your test methods:
public class TestClass {
private ExtentReports extent;
private ExtentTest test;
@BeforeTest
public void beforeTest() {
extent = new ExtentReports();
extent.attachReporter(new ExtentHtmlReporter("extent.html"));
}
@BeforeMethod
public void beforeMethod(Method method) {
test = extent.createTest(method.getName());
}
@Test
public void testMethod1() {
// your test logic
test.pass("Test passed");
}
@Test
public void testMethod2() {
// your test logic
test.fail("Test failed");
}
@AfterMethod
public void afterMethod(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
test.fail(result.getThrowable());
}
extent.flush();
}
}
In this example, an ExtentTest instance is created in the beforeMethod method using the name of the test method. The test results are added to the report using the pass and fail methods. The report is flushed and saved to the HTML file in the afterMethod method.
By following these steps, you can integrate Extent Reports into your TestNG tests and generate detailed and customizable test reports.