尝试使用断言运行AfterMethod

问题描述 投票:0回答:1

我的测试课中有很多测试方法。我的目标是能够使用断言两次:第一次在每次测试中,第二次在每次AfterMethod测试中。

这是我的代码示例:

 @AfterMethod(alwaysRun = true )
public void reportTestFail() {

   String a = getAllParameters().get("A");
   if (a.contains("1")) {
       asserter.fail("1 is found in parameters");
   }
   else {
       asserter.assertTrue(true,"Test passed");
   }
}

为什么每次测试结束时总是会出现配置失败的情况?

我不能在Test方法之外断言吗?

java selenium testng
1个回答
0
投票

在这种情况下,您应该使用TestNG IInvokedMethodListener2IInvokedMethodListener(对于TestNG版本7+)并在afterInvocation中进行额​​外验证。在这个方法的实现中,您应该在try-catch块中编写断言,在catch块中,您应该将testMethod状态设置为失败并设置异常。例如:

public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context){
 try{
  //perform addition validations
 }catch(Exception e){
   //assertion failed
   //update testResult to fail
 }
}

要修改测试用例的结果,请参阅qazxsw poi中的代码。

© www.soinside.com 2019 - 2024. All rights reserved.