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

发布于 2024-12-19 01:32:19 字数 230 浏览 3 评论 0原文

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

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

I'm testing extensively with JUnit and sometimes - while debugging my code - I want (temporary) only run a single @Test of my @RunWith(Arquillian.class) test class. Currently I'm adding a @Ignore to all other tests and wondering if something like @IgnoreOther does exist.

Are there better solutions to ignore all other tests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叫思念不要吵 2024-12-26 01:32:19

最简单的方法是将所有@Test替换为//###$$$@Test。然后,当调试完成后,将 //###$$$@Test 替换为 @Test

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

The simplest way is to replace all @Test to //###$$$@Test. Then when your debugging is finished replace //###$$$@Test to @Test.

Moreover typically IDEs allow running one test only. For example in Eclipse you can do it from Outline view.

感情旳空白 2024-12-26 01:32:19

只是我的两分钱。您可以尝试使用 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();
                }
            }
        };
    }
}

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");
    }

}

Just my two cents. You can try to use Junit Rules as @srkavin suggested.

Here is an example.

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();
                }
            }
        };
    }
}

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");
    }

}
素食主义者 2024-12-26 01:32:19

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

Test rules (JUnit 4.7+) will help. For example, you can write a rule that ignores all @Test methods except one with a specific name.

又怨 2024-12-26 01:32:19

来自 srkavin 的答案(和 mijer) 是正确的,但该代码从 JUnit 4.9 开始已弃用。接口和方法签名已更改。我想为其他对这个问题感兴趣的人提供这个。

public class IgnoreOtherRule implements TestRule
{
  private String applyMethod;

  public IgnoreOtherRule(String applyMethod){
    this.applyMethod = applyMethod;
  }

  @Override
  public Statement apply(final Statement statement, final Description description)
  {
    return new Statement()
    {
        @Override
        public void evaluate() throws Throwable {
            if (applyMethod.equals(description.getMethodName())) {
                statement.evaluate();
            }
        }
    };
  }
}

The answer from srkavin (and mijer) is correct, but the code is deprecated from JUnit 4.9. The interface and the method signature have changed. I want to provide this for others interested in this issue.

public class IgnoreOtherRule implements TestRule
{
  private String applyMethod;

  public IgnoreOtherRule(String applyMethod){
    this.applyMethod = applyMethod;
  }

  @Override
  public Statement apply(final Statement statement, final Description description)
  {
    return new Statement()
    {
        @Override
        public void evaluate() throws Throwable {
            if (applyMethod.equals(description.getMethodName())) {
                statement.evaluate();
            }
        }
    };
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文