将堆栈跟踪从 TestNG 中的@afterMethod写入范围报告



在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() + "nn" + 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让我更接近,但没有行号:

[测试结果名称=函数检查状态=失败 method=ExampleScripts.functionChecks(([pri:0, instance:_exampleScripts.exampleScripts@731a74c] 输出=已完成 执行以下方法:示例脚本.函数检查]

idk 如果这有帮助,但这对我有用......这是来自我的测试侦听器基类:

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

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

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

test.fail(result.getThrowable());

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

您可以使用:

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"方法,该方法给出了导致问题的可抛出物。您可以通过使用 Throwables.getStackTraceAsString(throwable( 转换为字符串来打印它

你也可以使用 result.getThrowable.getStackTrace((

来获取一个堆栈数组,你必须从中打印每个元素才能看到完整的数组。 例如:result.getThrowable.getStackTrace(([0] 打印第一行。

最新更新