How do Extent Reports generate an HTML report when the test fails outside the @Test tag?

Asked

Viewed 422 times

1

I created a report template for my test project that is up to now meeting my demand, however, if the test fails outside the@Test tag the report is not generated (being some connection error, or driver error, etc). I believe this is because the test does not pass the @Aftermethod and @Aftertest tags when errors like this happen, and therefore I cannot run the commands extent.flush() and extent.close() and the html file is not created. Any suggestions what I can do? Follow code in Java below:

@BeforeTest
public void startTest() {

    className = this.getClass().getName();
    String dateName = new SimpleDateFormat("dd-MM-yyyy hhmmss").format(new Date());
    String userDir = System.getProperty("user.dir");
    nomePasta = className.replace("MOBILEX_AUTOMACAO.TEST.", "") + " " + dateName;

    new File(userDir + "\\target\\reports\\" + nomePasta);

    extent = new ExtentReports(userDir + "\\target\\reports\\" + nomePasta + "\\"
            + className.replace("MOBILEX_AUTOMACAO.TEST.", "") + "REPORT.html", true);
    extent.addSystemInfo("Nome APP", "MobileX");

    extent.loadConfig(new File(userDir + "\\extent-config.xml"));

}

@AfterMethod
public void getResult(ITestResult result) throws Exception {

    if (result.getStatus() == ITestResult.FAILURE) {
        String screenshotPath = getScreenhot(result.getName());
        logger.log(LogStatus.FAIL, "Test Case Failed is " + result.getThrowable());
        logger.log(LogStatus.FAIL, "Test Case Failed is " + result.getName());

        logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
    } else if (result.getStatus() == ITestResult.SUCCESS) {
        logger.log(LogStatus.PASS, "Test Case passed is " + result.getName());
    }

    extent.endTest(logger);
    DriverFactory.killDriver();
}

@AfterTest
public void endReport() throws IOException {

    extent.flush();
    extent.close();

}

My test project is for Mobile, I am using Testng, version 6.10 for my Java tests, with Appium version 7.0.

1 answer

1


If you change that:

@AfterTest
public void endReport() throws IOException {
    extent.flush();
    extent.close();
}

For that reason:

@AfterTest(alwaysRun = true)
public void endReport() throws IOException {
    extent.flush();
    extent.close();
}

It will pass in flush and close, including you can also use on @Aftermethod. Follows the documentation: Testng

Browser other questions tagged

You are not signed in. Login or sign up in order to post.