如何在ExtentReports中添加跳过的测试用例?

时间:2018-08-02 07:51:25

标签: java selenium extentreports

我需要将所有跳过的测试用例添加到我的ExtentReports中。我该如何实现?

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

1 个答案:

答案 0 :(得分:1)

尝试实现ITestListener和以下方法:

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

}

所以实现看起来像这样:

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

}

这是所有测试的概括。

希望这会有所帮助,