我只希望我的测试方法的某些子集在生产环境中运行。我使用@ProdAllowed
注释来注释这些测试方法。我还编写了一个小的自定义JUnit运行程序,它覆盖了runChild方法,因此它在“PROD”环境中仅运行@ProdAllowed
方法:
public class ProdAwareRunner extends BlockJUnit4ClassRunner {
public ProdAwareRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
ProdAllowed annotation = method.getAnnotation(ProdAllowed.class);
String env = CreditCheckSuite.getEnv().trim();
if (annotation != null || "DEV".equalsIgnoreCase(env) || "UAT".equalsIgnoreCase(env)) {
super.runChild(method, notifier);
} else {
notifier.fireTestIgnored(null); // this probably needs to be changed
}
}
}
这非常有效,但是我想要更多一点 - 让这个跳过的测试方法在Eclipse中被标记为被忽略(现在它们被标记为不运行,这不是我想要的)
答案 0 :(得分:2)
您可以通过扩展rule
来撰写TestWatcherpublic class DoNotRunOnProd extends TestWatcher {
protected void starting(Description description) { {
ProdAllowed annotation = description.getAnnotation(ProdAllowed.class);
String env = CreditCheckSuite.getEnv().trim();
if ((annotation == null) && !"DEV".equalsIgnoreCase(env) && !"UAT".equalsIgnoreCase(env)) {
throw new AssumptionViolatedException("Must not run on production.")
}
}
}
并将其添加到您的测试中
public class Test {
@Rule
public final TestRule doNotRunOnProd = new DoNotRunOnProd();
...
}
答案 1 :(得分:0)
这已在TestNG Groups中实施(并积极使用):
public class Test1 {
@Test(groups = { "dev", "uat" })
public void testMethod1() {
}
@Test(groups = {"uat", "prod"} )
public void testMethod2() {
}
@Test(groups = { "prod" })
public void testMethod3() {
}
}