将堆栈跟踪写入TestNG中@afterMethod的Extent Reports

时间:2017-03-23 18:44:24

标签: java testng

在TestNG的@afterMethod中,我想捕获堆栈跟踪并将其包含在范围报告中,但是,我无法找到一个好的解决方案。我意识到打印堆栈跟踪有很多线程,但没有一个给我我想要的东西。我想要导致失败的方法和行号。我不在乎堆栈跟踪是否很大,因为我希望能够看到导致故障线路的原因。

以下是我写到范围报告的示例:

    @AfterMethod(alwaysRun = true)
    public void afterMethod(ITestResult result, Method method) throws InterruptedException {

        ExtentTest test = ExtentTestManager.getTest();

        // Get thread id
        long id = Thread.currentThread().getId();

        if (result.getStatus() != ITestResult.SKIP)
        {

            String os = StoredVariables.getos().get();

            String resultStatus = "";
            // Write passing tests to Extent Reports
            if (result.getStatus() == ITestResult.SUCCESS) {
                    test.log(LogStatus.PASS, "<span class='label success'>" + result.getName() + "</span>");
                    resultStatus = "Passed";
                    StoredVariables.getfailedTest().set(false);
                } 

            // Write failing tests to Extent Reports
            if (result.getStatus() == ITestResult.FAILURE) {
                test.log(LogStatus.FAIL, "<span class='label failure'>" + result.getName() + "</span>", "<pre>Results = " + result.getThrowable().getCause() + "\n\n" + result.getThrowable().getMessage() + "</pre>");
                resultStatus = "Failed";
                StoredVariables.getfailedTest().set(true);
            } 

            System.out.println("TEST RESULT: " + method.getName() + " - Thread " + id + " = " + resultStatus);

Thread.currentThread().getStackTrace()只是给了我&#34; [Ljava.lang.StackTraceElement; @ 2e1ef60&#34;

通过提供以下内容直接打印ItestResult result让我更接近,但没有行号:

  

[TestResult name = functionChecks status = FAILURE   方法= ExampleScripts.functionChecks()[PRI:0,   实例:_exampleScripts.ExampleScripts@731a74c] output =已完成   执行以下方法:ExampleScripts.functionChecks]

3 个答案:

答案 0 :(得分:1)

只需将throwable传递给日志,就会正确打印异常。

参考:http://extentreports.com/docs/versions/3/java/#logging-exceptions

test.fail(result.getThrowable());

该解决方案也可以在文档的TeatNG示例部分中找到。

答案 1 :(得分:1)

idk,如果有帮助,但是对我有用...这来自我的TestListener基类:

@Override
public void onTestFailure(ITestResult iTestResult){ 
 ExtentTestManager.getTest().log(LogStatus.FAIL,iTestResult.getThrowable().toString());
}

答案 2 :(得分:0)

您可以使用:

import com.google.common.base.Throwables;

//and in after method:
Throwables.getStackTraceAsString(result.getThrowable()));

完整方法:

@AfterMethod
    public void tearDown(ITestResult result) {

        System.out.println("Status is :" + result.getStatus());
        if (result.getStatus() == 1) {
            System.out.println("Passed");
            test.log(LogStatus.PASS, "Test Passed", test.addBase64ScreenShot(CaptureScreenShot.capture()));
        } else {

            test.log(LogStatus.FAIL, "Test Failed", test.addBase64ScreenShot(CaptureScreenShot.capture())+Throwables.getStackTraceAsString(result.getThrowable()));
        }

        driver.quit();

    }

ItestResult具有方法“ getThrowable”,该方法给出引发问题的throwable。您可以通过使用Throwables.getStackTraceAsString(throwable)转换为字符串来打印它

还可以使用result.getThrowable.getStackTrace()获取一个堆栈数组,您必须从该堆栈数组中打印每个元素以查看完整的数组。例如:result.getThrowable.getStackTrace()[0]打印第一行。