Java 在 testng @AfterMethod 中检测测试失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18597976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect test failure in testng @AfterMethod
提问by Ingo Kegel
I want to take a screenshot if a test fails. Rather than wrapping all test methods with try/catch blocks, I would like to add this logic to the method annotated with @AfterMethod
.
如果测试失败,我想截图。与其用 try/catch 块包装所有测试方法,我想将此逻辑添加到用@AfterMethod
.
How can I detect in the method annotated with @AfterMethod
if the current test has failed?
@AfterMethod
如果当前测试失败,如何在注释的方法中检测?
采纳答案by JacekM
If the method annotated with @AfterMethod
has an ITestResult
parameter then TestNG will automatically inject the result of the test. (source: TestNG documentation, section 5.18.1)
如果注解的方法@AfterMethod
有ITestResult
参数,TestNG 会自动注入测试结果。(来源:TestNG 文档,第 5.18.1 节)
This should do the job:
这应该可以完成这项工作:
@AfterMethod
public void tearDown(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
//your screenshooting code goes here
}
}
回答by niharika_neo
It would be good if you can implement a listener IInvokedMethodListenerand implement the afterInvocation()
method. This gives you access to the result object of your method. You can put your code to take a screenshot here.
如果您可以实现侦听器IInvokedMethodListener并实现该afterInvocation()
方法,那就太好了。这使您可以访问方法的结果对象。您可以在此处放置代码以截取屏幕截图。
回答by thekevinmonster
A good option is to use an ITestListener
instead of handling failure reporting in @AfterMethod
. The test listener's onTestFailed()
function will be called after your test method has run, but before the @AfterMethod
method has run. (Note that there is not an onTestFinished()
function in the listener; that role is fulfilled by @AfterMethod
.)
一个不错的选择是ITestListener
在@AfterMethod
. 测试侦听器的onTestFailed()
函数将在您的测试方法运行之后但在@AfterMethod
方法运行之前调用。(请注意,onTestFinished()
侦听器中没有函数;该角色由 完成@AfterMethod
。)
To make things easier on yourself, you can use a TestListenerAdapter
, which will implement all of the other functions that you don't specifically @Override
yourself.
为了让您自己更轻松,您可以使用 a TestListenerAdapter
,它将实现您自己没有专门使用的所有其他功能@Override
。
What you end up with is:
你最终得到的是:
public class MyTestListener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult result){
yourTakeScreenShotFunctionHere();
}
}
You then attach the listener to your test class with
然后您将侦听器附加到您的测试类
@Listeners({MyTestListener.class})
public class MyTestClass(){etc}
The ITestResult
is a reference to your test class's object, so you can use reflection to grab data out of it such as a Selenium WebDriver instance, etc.
这ITestResult
是对测试类对象的引用,因此您可以使用反射从中获取数据,例如 Selenium WebDriver 实例等。
You can have as many different listeners as you want, to do various things such as clean up or preserve error logs, take screenshots, trigger other reporting functionality, send emails, or whatever you want.
您可以根据需要拥有任意数量的不同侦听器,以执行各种操作,例如清理或保留错误日志、截取屏幕截图、触发其他报告功能、发送电子邮件或进行任何您想要的操作。
回答by Nathan Ripert
Variant to the previous answers:
与之前的答案不同:
Since you already know that you want to take the screenshot only in case of failure, you can use onTestFailure()
method from TestListenerAdapter
:
由于您已经知道您只想在失败的情况下截取屏幕截图,因此您可以使用以下onTestFailure()
方法TestListenerAdapter
:
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class LFailure extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult tr) {
System.out.println("Failure");
// your screenshot code
}
}
PS: don't forget to add LFailure
to your testng.xml, or directly in the code like this:
PS:不要忘记添加LFailure
到你的testng.xml中,或者直接在代码中这样:
@Listeners({ LFailure.class })
(preferably in a base class, from which all your tests inherit)
(最好在基类中,所有测试都继承自该基类)