sunpat
sunpat

Reputation: 399

Extent report shows only last test case result

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();
    }
}

If run these 2 test cases, I am getting only last testcase result, for the 1st testcase result, it's not displayed on the extent report. Note that there is not append parameter for extent report 3.0. How to get all test case results on extent report?

Only last test case result shown, how to get all tests results

Upvotes: 0

Views: 9081

Answers (4)

Sandeep b
Sandeep b

Reputation: 31

In your case ExtentManager.GetExtent() this method overrides the previously created report hence only the last test result shows up in report. Make sure this method is only called during the whole set of your test start, best way is to do it by implementing ITestListener

Upvotes: 1

sunpat
sunpat

Reputation: 399

I got one approach that works well, 1st check if the extent object is already created? if yes then return the object without reinitialising the extent object, here is how it looks

Under the ExtentManager class [as shown in the question], add this code block

public static ExtentReports getInstance() {
        if(extent == null) {
            GetExtent();
        }   
        return extent;
    }

Now under your testNG tests, before class annotation, call the above method

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

Upvotes: 1

Ganesh
Ganesh

Reputation: 21

Use this method extent.flush() in @aftersuite. because this statement generates the result

 {   
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");
    }

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

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

Upvotes: -1

Sakshi Singla
Sakshi Singla

Reputation: 2502

In the above approach, you are creating a new extent report in each Class. That is why you are getting only the latest executed test result.

You can create a common superclass for both TC1 and TC2 classes. In the superclass you can create @AfterClass and @BeforeClass functions. Then it should work.

Hope it helps!

Upvotes: 2

Related Questions