创建您自己的TestClassMethodsRunner(没有文档记录,或者我现在找不到)。
一个TestClassMethodsRunner 执行所有的TestCases,你可以设置一个过滤的TestClassMethodsRunner。
您所要做的就是覆盖TestMethodRunner createMethodRunner(Object, Method, RunNotifier) 方法。这是一个简单的 hacky 解决方案:
public class FilteredTestRunner extends TestClassMethodsRunner {
public FilteredTestRunner(Class<?> aClass) {
super(aClass);
}
@Override
protected TestMethodRunner createMethodRunner(Object aTest, Method aMethod, RunNotifier aNotifier) {
if (aTest.getClass().getName().contains("NOT")) {
return new TestMethodRunner(aTest, aMethod, aNotifier, null) {
@Override
public void run() {
//do nothing with this test.
}
};
} else {
return super.createMethodRunner(aTest, aMethod, aNotifier);
}
}
}
使用此 TestRunner,您可以执行所有不包含字符串“NOT”的测试。其他的将被忽略 :) 只需将 @RunWith 注释与您的 TestRunner 类一起添加到您的测试中。
@RunWith(FilteredTestRunner.class)
public class ThisTestsWillNOTBeExecuted {
//No test is executed.
}
@RunWith(FilteredTestRunner.class)
public class ThisTestsWillBeExecuted {
//All tests are executed.
}
在createMethodRunner 方法中,您可以对照必须执行的测试列表或引入新标准来检查当前测试。
祝你好运!
感谢您提供更好解决方案的提示!