In Eclipse, how do I run a JUnit test case multiple times

后端 未结 4 1495
深忆病人
深忆病人 2020-12-31 19:44

I have a unit test that fails sometimes and debugging it is a pain because I don\'t know why it sometimes fails.

Is there a way inside Eclipse that I can run a JUnit

相关标签:
4条回答
  • 2020-12-31 19:46

    There is a test decorator for this. See Junit API at http://junit.org/apidocs/junit/extensions/RepeatedTest.html

    for example

    @Test  
    @Repeat(10)  
    public void FailRandomlyNeedToKnowWhy() {  
        ....
    }
    
    0 讨论(0)
  • 2020-12-31 19:48

    I just found the following solution which doesn't require any additional depedency (Spring is required for one of the answers you got).

    Run your test with the Parameterized runner:

    @RunWith(Parameterized.class)
    

    Then add the following method to provide a number of empty parameters equals to the number of times you want to run the test:

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

    This way you don't even have to write a loop. IntelliJ and eclipse also group the results of every iteration together.

    0 讨论(0)
  • 2020-12-31 20:04

    Inspired on this solution:

    Use @Repeat annotation like this:

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

    You'll only need these two classes:

    Repeat.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 Edit: In order to use this solution when using @RunWith(PowerMockRunner.class), update to Powermock 1.6.5 (which includes this patch).

    0 讨论(0)
  • 2020-12-31 20:09

    Have you tried something like this?

    @Test
    public void runMultipleTests() {
        for (int i = 0; i < 10; i++) {
            myTestMethod();
        }
    }
    
    0 讨论(0)
提交回复
热议问题