如何从命令行获取测试失败消息 [英] how to get testng fail messages from command line

查看:23
本文介绍了如何从命令行获取测试失败消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 testng 的新手.当我从命令行运行它时,我想看到预期和实际消息.当我从我的 IDE 运行它时,它可以工作:

I'm new to testng. I want to see the Expected and Actual message when I run it from command line. When I run it from my IDE, it works:

java.lang.AssertionError:预期:3 实际:2

java.lang.AssertionError: Expected :3 Actual :2

当我使用以下命令从命令行运行它时:

when I run it from the command line using:

java org.testng.TestNG -testclass SimpleTest

我明白了:

命令行套件运行的测试总数:2,失败:2,跳过:0

Command line suite Total tests run: 2, Failures: 2, Skips: 0

(无消息)当我将测试包装在 try...catch 块中时,它看起来像:

(no message) when I wrap my test in a try... catch block so it looks like:

try{
   int x = 3;
   Assert.assertEquals(2,x);
} catch (AssertionError ae){
   System.out.println(ae.getMessage());
}

然后我收到我的消息.

expected:<3> but was:<2>

这似乎不是一种干净的测试编码方式.你会怎么做?

This seems like not a clean way to code a test. How would you do it?

推荐答案

TestNG 不提供此 OOB.默认情况下,它以 html 格式生成报告.

TestNG doesn't provide this OOB. By default it generates a report in html.

如果你想在控制台上写报告,你需要写一个海关TestListenerAdapter.

If you want to write a report on the console, you need write a customs TestListenerAdapter.

有一个关于如何在 TestNG 中做类似操作的小例子 logging文档.您可以使用相同的示例,并覆盖 onTestFailure(),读取测试结果,并从 ITestResult.getThrowable() 获取异常消息.

There's a small example of how to do something similar in the TestNG logging documentation. You can use the same example, and override onTestFailure(), read the result of the test, and get the exception message from ITestResult.getThrowable().

这篇关于如何从命令行获取测试失败消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆