ITestListener - ExtentReport

2019-06-12 04:49发布

问题:

Currently I'm using ExtentReport to generate automation reports.

The way I'm using ExtentReport is using the IReporter implementation to generate the report at the end of my tests, which is great.

However, now I'm looking at creating a way of monitoring my tests while they are being executed which is not possible with IReporter.

I want to create a separate listener for real time results using ITestListener.

Has anyone used this with ExtentReport? Or anything similar?

Any useful articals or guidelines in the right direction would be appreciated.

Thanks.

EDIT: Basically need a way to generate the ITestListener live console outputs to an actually HTML Reprot where I can view the test progress from there rather than the console

回答1:

It should look something like:

import com.relevantcodes.extentreports.*;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.internal.IResultListener;

import java.util.Arrays;
import java.util.Locale;

/**
 * Created by andrey.smirnov on 14.06.2016.
 */
public class ExtentListener implements IResultListener {

    private ExtentReports reporter =  new ExtentReports("build/SimpleReport.html", true, DisplayOrder.NEWEST_FIRST, NetworkMode.OFFLINE, Locale.ENGLISH);
    private ExtentTest testReporter;


    @Override
    public void onTestStart(ITestResult result) {
        testReporter = reporter.startTest(result.getMethod().getMethodName(), "Some description");
        testReporter.log(LogStatus.INFO, "Starting test " + result.getMethod().getMethodName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        testReporter.log(LogStatus.PASS, "Test PASSED");
        reporter.endTest(testReporter);
        reporter.flush();
    }

    @Override
    public void onFinish(ITestContext context) {
        reporter.close();
    }

   // Other interface methods
}

It will provide report updating on each test finish. Please refer to documentation about tests parallel running. Also it would be better to pass ExtentReports instance as TestNG context attribute e.g. iTestContext.setAttribute("reporter", reporter) and use it in listener.