Java 如何从@AfterMethod 中的 TestNG/Selenium 获取测试结果状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36125229/
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
How to get the test result status from TestNG/Selenium in @AfterMethod?
提问by hirosht
For a research I'm doing, I'm in need of capturing the result status (Passed/Failed) after running the test method (@Test), from @AfterMethod.
对于我正在进行的一项研究,我需要在运行测试方法 (@Test) 后从 @AfterMethod 捕获结果状态 (Passed/Failed)。
I have been using the import org.testng.ITestResult; as an out come of my research to get my work easier after going the several online blogs, but It seems like it didn't success my expectation as always the result outputs as passed, even though an assertion failed.
我一直在使用 import org.testng.ITestResult; 作为我的研究结果,在访问了几个在线博客后让我的工作更轻松,但似乎它并没有像我的期望那样成功,结果输出总是通过,即使断言失败。
My Code is as follows :
我的代码如下:
public class SampleTestForTestProject {
ITestResult result;
@Test(priority = 1)
public void testcase(){
// intentionally failing the assertion to make the test method fail
boolean actual = true;
boolean expected = false;
Assert.assertEquals(actual, expected);
}
@AfterMethod
public void afterMethod() {
result = Reporter.getCurrentTestResult();
switch (result.getStatus()) {
case ITestResult.SUCCESS:
System.out.println("======PASS=====");
// my expected functionality here when passed
break;
case ITestResult.FAILURE:
System.out.println("======FAIL=====");
// my expected functionality here when passed
break;
case ITestResult.SKIP:
System.out.println("======SKIP BLOCKED=====");
// my expected functionality here when passed
break;
default:
throw new RuntimeException("Invalid status");
}
}
}
Result in the Console :
结果在控制台:
[TestNG] Running: C:\Users\USER\AppData\Local\Temp\testng-eclipse--988445809\testng-customsuite.xml
======PASS=====
FAILED: testcaseFail
java.lang.AssertionError: expected [false] but found [true]
My expectation is to get the test result to a variable to get through the switch, as given in the above code snippet, and get printed "======FAIL=====" when the test method fail.
我的期望是将测试结果传递给一个变量以通过开关,如上面的代码片段所示,并在测试方法失败时打印“======FAIL=====”。
Will someone be able assist me kindly to catch the execution test result for each test method (@Test). If the method I have approached is wrong, please assist me with a code snippet to the correct approach, kindly.
有人可以帮助我捕捉每个测试方法(@Test)的执行测试结果。如果我采用的方法是错误的,请帮助我提供正确方法的代码片段。
Thank you in advance
先感谢您
采纳答案by noor
just do it:
去做就对了:
public class stacktest {
@Test
public void teststackquestion() {
boolean actual = true;
boolean expected = false;
Assert.assertEquals(actual, expected);
}
@AfterMethod
public void afterMethod(ITestResult result)
{
try
{
if(result.getStatus() == ITestResult.SUCCESS)
{
//Do something here
System.out.println("passed **********");
}
else if(result.getStatus() == ITestResult.FAILURE)
{
//Do something here
System.out.println("Failed ***********");
}
else if(result.getStatus() == ITestResult.SKIP ){
System.out.println("Skiped***********");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
回答by David Baak
The TestListenerAdapter
has methods for each of those situations (success, skipped, failure). My suggestions is to make your own listener like this.
在TestListenerAdapter
对每个这样的情况下,方法(成功,跳过,失败)。我的建议是让你自己的听众像这样。
public class MyTestResultListener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult result) {
// do what you want to do
}
@Override
public void onTestSuccess(ITestResult result) {
// do what you want to do
}
@Override
public void onTestSkipped(ITestResult result) {
// do what you want to do
}
}
Then add your listener to the test class.
然后将您的侦听器添加到测试类。
@Listeners(MyTestResultListener.class)
public class MyTest {
// your tests
}