跳过执行期间抛出的具体异常的 TestNG 测试

Skip TestNG test on concrete exception thrown during execution

这与问题How to create own annotation for junit that will skip test if concrete exception was thrown during execution?有关,但对于TestNG。

我寻求一种解决方案,将 TestNG 集成测试配置为在已知的基础设施异常列表上跳过(而不是失败),这些异常有时会出现。

浏览完 TestNG 代码后,我了解到我无法使用 IHookable 或其他侦听器实现该目的。此外,TestNG 没有任何类似于 JUnit 的 @Rule TestRule 对象的东西。此外,使所有基础设施例外以扩展 TestNG 的 SkipException 对我来说不是一个选择。

我是不是漏掉了什么?

实施IInvokedMethodListener并在afterInvocation方法中做类似

的事情
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

if(testResult.getThrowable().getClass().equals(YourExceptionClass.class))
     testResult.setStatus(TestResult.SKIP);
}

在您的 testng.xml 中或根据您执行测试的方式设置此侦听器。