JUnit @Ignore所有其他测试(@IgnoreOther?)

Tho*_*hor 11 java junit junit4 jboss-arquillian

我正在使用JUnit进行广泛测试,有时候 - 在调试我的代码时 - 我想(临时)只运行@Test我的一个@RunWith(Arquillian.class)测试类.目前我正在添加一个@Ignore到所有其他测试,并想知道是否@IgnoreOther存在类似的东西.

是否有更好的解决方案忽略所有其他测试?

szh*_*hem 10

只是我的两分钱.您可以尝试使用Junit规则,如@srkavin建议的那样.

这是一个例子.

package org.foo.bar;

import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

public class SingleTestRule implements MethodRule {
    private String applyMethod;
    public SingleTestRule(String applyMethod) {
        this.applyMethod = applyMethod;
    }
    @Override
    public Statement apply(final Statement statement, final FrameworkMethod method, final Object target) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                if (applyMethod.equals(method.getName())) {
                    statement.evaluate();
                }
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)
package org.foo.bar;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

public class IgnoreAllTest {

    @Rule
    public SingleTestRule test = new SingleTestRule("test1");

    @Test
    public void test1() throws Exception {
        System.out.println("test1");
    }

    @Test
    public void test2() throws Exception {
        Assert.fail("test2");
    }

    @Test
    public void test3() throws Exception {
        Assert.fail("test3");
    }

}
Run Code Online (Sandbox Code Playgroud)


Ale*_*exR 5

最简单的方法是将所有替换@Test//###$$$@Test.然后在调试完成后替换//###$$$@Test@Test.

此外,IDE通常只允许运行一个测试.例如,在Eclipse中,您可以从"大纲"视图中执行此操作.


srk*_*vin 5

测试规则(JUnit 4.7+)会有所帮助.例如,您可以编写一个忽略所有@Test方法的规则,除了具有特定名称的方法.