java - 尝试使用断言运行 AfterMethod
问题描述
我的测试课上有很多测试方法。我的目标是能够使用两次断言:第一次在每个测试中,第二次在每个 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");
}
}
为什么每次测试结束时我总是配置失败?
我不能在测试方法之外断言吗?
解决方案
在这种情况下,您应该使用 TestNG IInvokedMethodListener2或IInvokedMethodListener
(对于 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
}
}
要修改测试用例的结果,请参考qaf中的代码。
推荐阅读
- java - 当我使用 webflux 和 hatoas 时不显示绝对路径
- javascript - 如何循环消息收集器?
- php - 在 Laravel 中使用不同的外键创建变形关系
- json - Amazon ECS 容器运行状况检查
- django - StreamingHttpResponse 不适用于 ASGI,但可以正常使用 WSGI
- blazor - 在 Linux CloudFoundary 上获取 _blazor/negotiate 禁止错误 403
- python - [自定义层]ValueError: tf.function-decorated 函数试图在非首次调用时创建变量
- flutter - 即使Firestore有数据,也会颤动Firestore getter docs null
- apache - Apache 从哪里读取 $domain 变量?
- react-native - 如何使用 Vanilla JavaScript 和 MS Graph API 从 OneDrive 下载文件