让TestExecutionListener跳过/忽略测试
问题描述:
我目前使用Spring TestExecutionListener来处理我的测试类中的自定义注释。根据一些外部情况,我想忽略一个测试。让TestExecutionListener跳过/忽略测试
目前我使用beforeTestClass
或beforeTestMethod
中的某个假设来实现此功能。但不幸的是,它在日志中创建了一个警告,包括一个远离嘈杂的堆栈跟踪。
是否有可能以编程方式忽略测试(可能使用TestContext)?
答
我想你可以尝试扩展默认的测试运行,并提供isIgnored
方法的自定义实现:
public class CustomRunner extends BlockJUnit4ClassRunner {
public CustomRunner(Class<?> c) throws initializationError {
super(c);
}
@Override
protected boolean isIgnored(FrameworkMethod child) {
if(shouldIgnore()) {
return true;
}
return super.isIgnored(child);
}
private boolean shouldIgnore() {
/* some custom criteria */
}
}
这里,base implementation忽略具有@Ignore
注释测试,但您可以提供自己的实现的忽略标准。
难道你不能仅仅为测试添加'@ActiveProfiles(profiles = {“dev”}'并关闭配置文件吗? – StanislavL
由于复杂的硬件需求,如果没有复杂化就不可能实现,因此我们创建了自己的注解根据所连接的设备跳过测试 – Nitek