如何根据条件自动跳过某些JUnit测试? [英] How can I automatically skip certain JUnit tests based on a condition?

查看:729
本文介绍了如何根据条件自动跳过某些JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种简单的方法为我的JUnit测试分配优先级值,这样我可以说仅运行优先级1测试",运行优先级1、2和3测试"等.我知道我可以在每个测试的开始处都包含类似Assume.assumeTrue("Test skipped for priority " + priority, priority <= 2);的行(其中priority是我要运行的最高优先级测试,而2是此特定测试的优先级值),但是请复制粘贴该行每次测试的开始似乎都不是一个很好的解决方案.

I would like a simple way to assign a priority value to my JUnit tests such that I can say 'run only priority 1 tests', 'run priority 1, 2 and 3 tests' etc. I am aware that I could just include a line like Assume.assumeTrue("Test skipped for priority " + priority, priority <= 2); at the start of each test (where priority is the highest value priority tests I want to run and 2 is the priority value of this particular test), however copy-pasting a line at the start of each test doesn't seem a very good solution.

我尝试使用简单的注解来编写解决方案,该注解由我一直在使用的JUnit规则检测到:

I have tried writing a solution using a simple annotation which is detected by the JUnit Rule I am using anyway:

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()中的任何代码仍在运行.

While this appears to work (the correct tests are shown as skipped in the Eclipse JUnit view) the tests are still performed, i.e. any code in test1() is still run.

有人知道我如何在规则中获得Assume来真正跳过测试吗?

Does anyone have any idea how I can get the Assume in my Rule to actually skip the test?

推荐答案

TestWatcher.starting引发的异常将被忽略,并在测试结束时重新抛出.

The exception thrown by a TestWatcher.starting are ignored, and rethrown at the end of the test.

您应该实现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测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆