Extent Report With Multiple Classes
Extent Report with Multiple classes will discuss about how to create a single report when you have multiple classes. Usually people will face issue that they will get the report with single class as all the extent reports related classes will be in the same class but when they have multiple classes then they might not get the single report for all the methods which are there in the different classes.
To overcome this issue, we need to create a separate class to declare the extent reports related classes and need to extend that class to access the object of those classes in each class where we need to generate the report for the test methods.
To show this, here we will create a class(i.e. BaseTest) to declare the extent reports related classes and will create 2 more classes(i.e. Functionality1 and Functionality2) to access these objects and will create a single report from these two classes.
The most important thing here is, once you create an object to the extent report classes we need to access the same object in different classes for this we have declare these variables as static then all the instances share the same copy of that variable. And a class variable can be accessed directly with the class, without the need to create an instance.
Below is the BaseTest class where we can declare the classes which are required to generate the extent reports and will configure the basic information for the report how it should generate. As we know that extent reports will use TestNG to generate the reports. Basic configuration is single time activity so we will put that code in @BeforeSuite and @AfterSuite annotations. And will run the code using testng.xml file as we need to run multiple classes at a time.
import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.Status; import com.aventstack.extentreports.markuputils.ExtentColor; import com.aventstack.extentreports.markuputils.MarkupHelper; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import com.aventstack.extentreports.reporter.configuration.ChartLocation; import com.aventstack.extentreports.reporter.configuration.Theme; public class BaseTest { public static ExtentHtmlReporter htmlReporter; public static ExtentReports extent; public static ExtentTest test; @BeforeSuite public void setUp() { htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/MyOwnReport.html"); extent = new ExtentReports(); extent.attachReporter(htmlReporter); extent.setSystemInfo("OS", "Mac Sierra"); extent.setSystemInfo("Host Name", "Krishna"); extent.setSystemInfo("Environment", "QA"); extent.setSystemInfo("User Name", "Krishna Sakinala"); htmlReporter.config().setChartVisibilityOnOpen(true); htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report"); htmlReporter.config().setReportName("My Own Report"); htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP); htmlReporter.config().setTheme(Theme.DARK); } @AfterMethod public void getResult(ITestResult result) { if(result.getStatus() == ITestResult.FAILURE) { test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED)); test.fail(result.getThrowable()); } else if(result.getStatus() == ITestResult.SUCCESS) { test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN)); } else { test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE)); test.skip(result.getThrowable()); } } @AfterSuite public void tearDown() { extent.flush(); } }
Below is the class1 (i.e. Functionality1) which will have actual test cases/scripts and need to generate the report for the status of the test methods under this class. So, we will extend the BaseTest to this class to access the extent report related classes.
import org.testng.Assert; import org.testng.annotations.Test; public class Functionality1 extends BaseTest { @Test public void functionality1Test1() { test = extent.createTest("functionality1Test1"); Assert.assertTrue(1 > 0); } @Test public void functionality1Test2() { test = extent.createTest("functionality1Test2"); Assert.assertEquals("Krishna", "Sakinala"); } @Test public void functionality1Test3() { test = extent.createTest("functionality1Test3"); Assert.assertNotEquals("Krishna", "Krishna"); } }
Below is the class2 (i.e. Functionality2) which will have actual test cases/scripts and need to generate the report for the status of the test methods under this class. So, we will extend the BaseTest to this class to access the extent report related classes.
import org.testng.Assert; import org.testng.annotations.Test; public class Functionality2 extends BaseTest { @Test public void functionality2Test1() { test = extent.createTest("functionality2Test1"); Assert.assertTrue(1 > 0); } @Test public void functionality2Test2() { test = extent.createTest("functionality2Test2"); Assert.assertEquals("Krishna", "Sakinala"); } @Test public void functionality2Test3() { test = extent.createTest("functionality2Test3"); Assert.assertNotEquals("Krishna", "Krishna"); } }
Now we will run the both classes using testng.xml file and will get the consolidated report for both the classes. Can find the same report below:
This way we can generate the single report from multiple classes.
Please watch the YouTube video of this blog for better understanding.