为了进行研究,我需要从@AfterMethod运行测试方法(@Test)后捕获结果状态(“通过” /“失败”)。
我一直在使用导入org.testng.ITestResult; 我的研究成果使访问多个在线博客后使我的工作变得更加轻松,但是,即使断言失败了,也似乎并没有像预期的那样成功,因为结果输出总是通过。
我的代码如下:
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");
}
}
}
结果在控制台中:
[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]
我的期望是将测试结果转换为变量以通过开关,如上面的代码片段所示,并在测试方法失败时显示“ ====== FAIL =====”。
有人能够帮助我捕获每种测试方法(@Test)的执行测试结果。如果我采用的方法不正确,请帮助我提供一段代码,以正确的方法。
先感谢您
去做就对了:
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();
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句