如何根据条件自动跳过某些 JUnit 测试?
How can I automatically skip certain JUnit tests based on a condition?
我想要一种简单的方法来为我的 JUnit 测试分配一个优先级值,这样我就可以说 'run only priority 1 tests'、'run priority 1, 2 and 3 tests' 等。我知道我可以只包含像 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
真正跳过测试?
TestWatcher.starting
抛出的异常被忽略,并在测试结束时重新抛出。
您应该实施 TestRule
而不是 TestWatcher
:
public class TestRules implements TestRule {
private int priority = 1; // this value is manually changed to set the priority of tests to run
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Priority testCasePriority = desc.getAnnotation(Priority.class);
Assume.assumeTrue("Test skipped for priotity " + priority, testCasePriority == null || testCasePriority.value() <= priority);
base.evaluate();
}
};
}
}
我想要一种简单的方法来为我的 JUnit 测试分配一个优先级值,这样我就可以说 'run only priority 1 tests'、'run priority 1, 2 and 3 tests' 等。我知道我可以只包含像 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
真正跳过测试?
TestWatcher.starting
抛出的异常被忽略,并在测试结束时重新抛出。
您应该实施 TestRule
而不是 TestWatcher
:
public class TestRules implements TestRule {
private int priority = 1; // this value is manually changed to set the priority of tests to run
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Priority testCasePriority = desc.getAnnotation(Priority.class);
Assume.assumeTrue("Test skipped for priotity " + priority, testCasePriority == null || testCasePriority.value() <= priority);
base.evaluate();
}
};
}
}