anshooarora / extentreports-java

Community version of Extent API for Java has moved to https://github.com/extent-framework/
http://extentreports.com
Other
528 stars 318 forks source link

Extent report shows only last test case result #737

Closed sunilpatro1985 closed 7 years ago

sunilpatro1985 commented 7 years ago

Extent report version - 3.0 Language - Java and TestNG classes

I have a class - ExtentManager.java

```package framewrk;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class ExtentManager {

    private static ExtentReports extent;
    private static ExtentTest test;
    private static ExtentHtmlReporter htmlReporter;
    private static String filePath = "./extentreport.html";

    public static ExtentReports GetExtent(){
        extent = new ExtentReports();

        htmlReporter = new ExtentHtmlReporter(filePath);

        // make the charts visible on report open
        htmlReporter.config().setChartVisibilityOnOpen(true);

        // report title
        String documentTitle = prop.getProperty("documentTitle", "aventstack - Extent");
        htmlReporter.config().setDocumentTitle(documentTitle);
}

    public static ExtentTest createTest(String name, String description){
        test = extent.createTest(name, description);
        return test;
    }

    public static ExtentTest createTest(String name){
        test = extent.createTest(name, "");
        return test;
    }
}```

and 2 testNG classes as follows
TC1.java

```package framewrk;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

public class TC1 {
    static ExtentReports extent;
    static ExtentTest test;

    @BeforeClass
    public void setup(){
        extent = ExtentManager.GetExtent();
    }

    @Test
    public void OpenUT(){
        test = extent.createTest("Testing how fail works");
        test.log(Status.INFO, "fail check started");
        test.fail("Test fail");
    }

    @AfterClass
    public void tear()
    {
        extent.flush();
    }
}```

TC2.java

```package framewrk;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

public class TC2 {
    static ExtentReports extent;
    static ExtentTest test;

    @BeforeClass
    public void setup(){
    extent = ExtentManager.GetExtent();
    }

    @Test
    public void OpenUT(){
        test = extent.createTest("Testing how pass works");
        test.log(Status.INFO, "pass check started");
        test.pass("Passed");
    }

    @AfterClass
    public void tear()
    {
        extent.flush();
    }
}```

testNG.xml

```<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MySuite">
  <test name="TestExtent">
    <classes>
      <class name="framewrk.TC1"/>
      <class name="framewrk.TC2"/>
    </classes>
  </test> 
</suite>```

Result only shows for TC2.java, how can I get result for both TC1.java and TC2.java [refer screenshot] extentresult

savvyaxnjxn commented 7 years ago

I think what may be missing is a call to append html reporters to each other as they run. See feature #668 and 3.0.1 documentation for setAppendExisting.

sunilpatro1985 commented 7 years ago

Yeah I tried this setAppendExisting() in 3.0.1, but was thinking if we could do the same for 3.0, if we can share extent object across different testng classes, but no idea if we can do this or not.

savvyaxnjxn commented 7 years ago

Hm... You have a good point; I only recently picked this tool up as of 3.0.1 (not 3.0 as you did mention in your OP). I'll share what I know in the hopes that it might be relevant to 3.0, but I am no expert in the feature set of 3.0 vs 3.0.1.

I can verify that we can do this in 3.0.1 (I use this maven dependency tag)

        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.0.1</version>
        </dependency>

I have a single report appending throughout a single suite running multiple suites running multiple tests running multiple classes and they all append to a single (fairly large) report. This can be run over and over again and they ALL get appended for days on end. I have it set up in my testng annotation '@BeforeClass' like this:

@BeforeClass
    public synchronized void classSetup){
        extentHtmlReportFile = "../extentReport/extentHtmlReportFile.html";
        htmlReporter = new ExtentHtmlReporter(extentHtmlReportFile);
        htmlReporter.loadXMLConfig("./extentConfig.xml");

        //append test report is TRUE; this maintains a history
        htmlReporter.setAppendExisting(true);
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        }
    }

I hope this helps! Good luck~

anshooarora commented 7 years ago

Everytime you are calling GetExtent, a new instance is returned. This isn't a good way of working on a "single" report. Instead, you need to create the instance once and utilize it for all your classes:

private static ExtentReports extent;

public static synchronized ExtentReports GetExtent() {
    if (extent != null) return extent;

    extent = new ExtentReports();
    ...
}