【问题标题】:In Eclipse, how do I run a JUnit test case multiple times在 Eclipse 中,如何多次运行 JUnit 测试用例
【发布时间】:2012-02-06 23:37:43
【问题描述】:

我有一个单元测试有时会失败,调试它很痛苦,因为我不知道为什么它有时会失败。

在 Eclipse 中是否有一种方法可以让我运行 JUnit 测试 5 次或 50 次之类的?

谢谢。

【问题讨论】:

  • 你要测试什么?
  • 恕我直言,单元测试“有时”失败是没用的。
  • 我的也是。这就是我想要修复它的原因。我需要多次运行它才能诊断问题。
  • @Thom:抱歉,误解了你的问题。
  • 也可以使用注解@Repeat(5) 运行五次

标签: java eclipse junit


【解决方案1】:

我刚刚找到了以下不需要任何额外依赖的解决方案(您得到的答案之一需要 Spring)。

使用Parameterized runner 运行您的测试:

@RunWith(Parameterized.class)

然后添加如下方法,提供多个空参数等于你要运行测试的次数:

@Parameterized.Parameters
public static List<Object[]> data() {
    return Arrays.asList(new Object[10][0]);
}

这样您甚至不必编写循环。 IntelliJ 和 eclipse 还将每次迭代的结果组合在一起。

【讨论】:

    【解决方案2】:

    你尝试过这样的事情吗?

    @Test
    public void runMultipleTests() {
        for (int i = 0; i < 10; i++) {
            myTestMethod();
        }
    }
    

    【讨论】:

    • 啊。有一个想法。如果我绝望了,我可能会尝试。
    【解决方案3】:

    为此有一个测试装饰器。请参阅 Junit API http://junit.org/apidocs/junit/extensions/RepeatedTest.html

    例如

    @Test  
    @Repeat(10)  
    public void FailRandomlyNeedToKnowWhy() {  
        ....
    }
    

    【讨论】:

    • RepeatedTest 装饰器来自 JUnit,而您的代码示例包含 Spring 注释,不是吗?
    【解决方案4】:

    灵感来自this solution:

    像这样使用@Repeat 注解:

    public class MyTestClass {
    
        @Rule
        public RepeatRule repeatRule = new RepeatRule();
    
        @Test
        @Repeat(10)
        public void testMyCode() {
            //your test code goes here
        }
    }
    

    你只需要这两个类:

    重复.java:

    import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
    import static java.lang.annotation.ElementType.METHOD;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention( RetentionPolicy.RUNTIME )
    @Target({ METHOD, ANNOTATION_TYPE })
    public @interface Repeat {
        int value() default 1;
    }
    

    RepeatRule.java:

    import org.junit.rules.TestRule;
    import org.junit.runner.Description;
    import org.junit.runners.model.Statement;
    
    public class RepeatRule implements TestRule {
    
        private static class RepeatStatement extends Statement {
            private final Statement statement;
            private final int repeat;    
    
            public RepeatStatement(Statement statement, int repeat) {
                this.statement = statement;
                this.repeat = repeat;
            }
    
            @Override
            public void evaluate() throws Throwable {
                for (int i = 0; i < repeat; i++) {
                    statement.evaluate();
                }
            }
    
        }
    
        @Override
        public Statement apply(Statement statement, Description description) {
            Statement result = statement;
            Repeat repeat = description.getAnnotation(Repeat.class);
            if (repeat != null) {
                int times = repeat.value();
                result = new RepeatStatement(statement, times);
            }
            return result;
        }
    }
    

    2016-10-25 编辑: 为了在使用@RunWith(PowerMockRunner.class) 时使用此解决方案,请更新为Powermock 1.6.5其中包括this patch)。

    【讨论】:

    • 我花了一点时间来验证它是否正在重新运行,但我注意到 1 次与 1000 次运行的时间从 6 秒增加到 2 分钟。但是,它实际上并没有为每个创建一个新的运行,这导致我删除了我拥有的超时规则。
    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2021-09-10
    • 2019-01-30
    • 1970-01-01
    相关资源
    最近更新 更多