我发现有这种行为的最好方法,并让他们在报告中跳过对认知的测试,就是使用你自己的跑步者(就像在AlexR的答案中一样),但是覆盖runChild方法,允许测试被选中但被处理为一个忽略,并没有完全排除。
注释使用
@Retention(RetentionPolicy.RUNTIME)
public @interface TargetOS {
String family();
String name() default "";
String arch() default "";
String version() default "";
}
的JUnit运行
public class OSSensitiveRunner extends BlockJUnit4ClassRunner {
public OSSensitiveRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (method.getAnnotation(Ignore.class) != null) {
notifier.fireTestIgnored(description);
} else if (method.getAnnotation(TargetOS.class) != null) {
final TargetOS tos = method.getAnnotation(TargetOS.class);
String name = tos.name().equals("") ? null : tos.name();
String arch = tos.arch().equals("") ? null : tos.arch();
String version = tos.version().equals("") ? null : tos.version();
if (OS.isOs(tos.family(), name, arch, version)) {
runLeaf(methodBlock(method), description, notifier);
} else {
notifier.fireTestIgnored(description);
}
} else {
runLeaf(methodBlock(method), description, notifier);
}
}
}
用法在测试
@RunWith(OSSensitiveRunner.class)
public class SeleniumDownloadHelperTest {
...
并限制特定的方法
@Test
@TargetOS(family = "windows")
public void testGetFileFromUrlInternetExplorer() throws Exception {
...
}
你能不能用[类别]更多的信息(https://github.com/KentBeck/junit/wiki/Categories)? – assylias