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

问题描述 投票:2回答:2

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

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

    @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()只给了我“[Ljava.lang.StackTraceElement; @ 2e1ef60”

通过提供以下信息,直接打印出ItestResult result可以让我更接近,但没有行号:

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

java testng
2个回答
1
投票

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

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

1
投票

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

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

test.fail(result.getThrowable());

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

© www.soinside.com 2019 - 2024. All rights reserved.