执行 TestNG 测试时捕获 Log4j 输出
我正在执行 TestNG 测试,并且日志输出设置为 DEBUG,因此如果发生故障,我可以准确检查出了什么问题。
问题是输出非常冗长,运行时会困扰每个人。我想捕获所有 Log4J 日志记录事件 - 这很简单 - 并且仅在测试失败时打印它们。另外,我需要考虑 @Before/@After
方法并打印它们的输出。
假设我已经有 Log4J LoggingEvent
列表,如何仅在 Test
/After
/Before< 时打印这些列表/code> 方法失败?
I am executing TestNG tests , and the logging output is set to DEBUG
, so in case of a failure I can get to inspect exactly what goes wrong.
The problem is that the output is extremely verbose, and it's bothering everyone when it runs . I would like to capture all Log4J logging events - which is easy - and only print them when the test fails. Also, I need to take into account @Before/@After
methods and also print output for them.
Assuming that I already have a List of Log4J LoggingEvent
s , how can I print those only when the Test
/After
/Before
methods fail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 Reporter.log(str) 将消息记录在报告上。
Use Reporter.log(str) to log the message on the report.
这个网站有一个解释关于如何做到这一点。。我在这里复制了代码部分,以防链接失效。
assertThat(arguments.getValue().getLevel(), is(Level.WARN));
This site has a explanation on how to do it.. I copied the code part here in case the link goes dead.
assertThat(arguments.getValue().getLevel(), is(Level.WARN));
实现并注册 org.testng。 ITestListener 并对回调方法做出反应。
Implement and register a org.testng.ITestListener and react on the callback methods.
将 JMockit 放入依赖项中。有了它,日志测试就变得非常容易。
放入测试类:
放入测试:
Put JMockit in dependencies. With it logging testing is very easy.
Put in test class:
Put in test:
您可以像下面的代码一样使用:
You can use like the below code: