【发布时间】:2016-05-27 15:01:49
【问题描述】:
我想要一种简单的方法来为我的 JUnit 测试分配优先级值,这样我就可以说“仅运行优先级 1 测试”、“运行优先级 1、2 和 3 测试”等。我知道我可以在每个测试开始时包括像Assume.assumeTrue("Test skipped for priority " + priority, priority <= 2); 这样的行(其中priority 是我想要运行的最高优先级测试,2 是这个特定测试的优先级值),但是在每个测试的开始似乎都不是一个很好的解决方案。
我尝试使用一个简单的注释来编写解决方案,该注释由我正在使用的 JUnit 规则检测到:
public class Tests {
@Rule
public TestRules rules = new TestRules();
@Test
@Priority(2)
public void test1() {
// perform test
}
}
public class TestRules extends TestWatcher {
private int priority = 1; // this value is manually changed to set the priority of tests to run
@Override
protected void starting(Description desc) {
Priority testCasePriority = desc.getAnnotation(Priority.class);
Assume.assumeTrue("Test skipped for priotity " + priority, testCasePriority == null || testCasePriority.value() <= priority);
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Priority {
public int value() default 0;
}
虽然这似乎有效(正确的测试在 Eclipse JUnit 视图中显示为已跳过),但测试仍在执行,即 test1() 中的任何代码仍在运行。
有谁知道如何让我的规则中的Assume 真正跳过测试?
【问题讨论】:
-
这可能会有所帮助:- stackoverflow.com/questions/1689242/…
-
... 或者这可能会有所帮助:codeaffine.com/2013/11/18/…
标签: java junit junit-rule