Search code examples
javaseleniumextentreports

How can I add skipped test cases in ExtentReports?


I need to add all of my skipped test cases to my ExtentReports. How can I achieve this?

I have tried below code in my BaseTest.java:

@AfterMethod
public void afterMethod(Method method)
{
    if (result.getStatus() == ITestResult.FAILURE) 
    {
        ((ExtentTest) extentTest.getTest()).log(LogStatus.FAIL, result.getThrowable());
    } 
    else
        if (result.getStatus() == ITestResult.SKIP)
        {
            ((ExtentTest) extentTest.getTest()).log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
        }
        else 
        {
            ((ExtentTest) extentTest.getTest()).log(LogStatus.PASS, "Test passed");
        }

    report.endTest(extentTest);
    report.flush(); 
}

Solution

  • Try implementing ITestListener and following methods:

      public interface ITestListener extends ITestNGListener {
    
      void onTestStart(ITestResult result);
    
      public void onTestSuccess(ITestResult result);
    
      public void onTestFailure(ITestResult result);
    
      public void onTestSkipped(ITestResult result);
    
      public void onTestFailedButWithinSuccessPercentage(ITestResult result);
    
      public void onStart(ITestContext context);
    
      public void onFinish(ITestContext context);
    
    }
    

    so implementation looks something like this:

    public class test implements ITestListener {
    
        @Override
        public void onTestSkipped(ITestResult result) {
            ... do Your stuff for skip tests extent report here ...
        }
    
        @Override
        public void onTestSuccess(ITestResult result) {
            ... do Your stuff for passed test extent report here ...
        }
    
        ... and all other methods are automatically implemented, and You don't have to check status, test will automatically enter proper methods. 
    
    
        @Override
        public void onStart(ITestContext context){
              //init extent reports... on whatever way You do it...
        }
    
    
        @Override
        public void onFinish(ITestContext context){
            report.endTest(extentTest);
            report.flush(); 
        }
    
    }
    

    And this is generalised for all test.

    Hope this helps,