0

I have a method called in my @AfterMethod in TestNG attempting to gather the counts of tests passed/failed/skipped, and in @AfterSuite printing out the counts. This also includes setting custom extent report status. When I run the Test Suite, I am getting a different count from ITestResult versus what is there in the logs (using maven surefire plugin). See below the method and the difference in counts:

@AfterMethod:

@AfterMethod
public void closure(ITestResult res) {
    success = 0;
    failed = 0;
    skipped = 0;

    if (res.getStatus() == ITestResult.FAILURE) {
        String failScreenshotPath = ExtentCustom.getScreenshot(res.getName());
        ExtentCustom.eachTest.log(Status.FAIL, ExtentCustom.getFailFont("Snapshot below : " + res.getThrowable()),
                MediaEntityBuilder.createScreenCaptureFromPath(failScreenshotPath).build());
        failed = res.getTestContext().getFailedTests().size();
        failed++;
    } else if (res.getStatus() == ITestResult.SUCCESS) {
        String passScreenshotPath = ExtentCustom.getScreenshot(res.getName());
        ExtentCustom.eachTest.log(Status.PASS, ExtentCustom.getPassFont("Snapshot below : "),
                MediaEntityBuilder.createScreenCaptureFromPath(passScreenshotPath).build());
        success = res.getTestContext().getPassedTests().size();
        success++;
    } else {
        if (res.getStatus() == ITestResult.SKIP) {
            ExtentCustom.eachTest.log(Status.SKIP, ExtentCustom.getInfoFont("Test Skipped"));
            skipped = res.getTestContext().getSkippedTests().size();
            skipped++;
        }
    }
}

@AfterSuite method:

@AfterSuite
public void reportFlush() throws IOException, InterruptedException {
    ExtentCustom.reportClosure();
    int total = success + failed + skipped;
    System.out.println("Total: " + total + System.lineSeparator() + "Passed: " + success + System.lineSeparator()
            + "Failed: " + failed + System.lineSeparator() + "Skipped: " + skipped);
}

Logs:

Total: 71
Passed: 71
Failed: 0
Skipped: 0
[ERROR] Tests run: 71, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 179.7 s <<< FAILURE! -- in TestSuite

Is there a reason for the count difference? The failure is coming from Selenium, but it seems to be disconnected from the TestNG result. Please let me know for any suggestions.

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
wheelerlc64
  • 435
  • 2
  • 5
  • 17

0 Answers0