【问题标题】:JUnit repeat annotation does not start test caseJUnit 重复注解不启动测试用例
【发布时间】:2016-09-13 17:33:11
【问题描述】:

我有一个 @Repeat 注释,用于重复运行 JUnit 测试。代码取自这篇博文 here,并经过修改以使用 JUnit 4.10 运行。

重复.java

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Repeat {
    int value();
}

ExtendedRunner.java

public class ExtendedRunner extends BlockJUnit4ClassRunner {

    public ExtendedRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Description describeChild(FrameworkMethod method) {
        if (method.getAnnotation(Repeat.class) != null &&
                method.getAnnotation(Ignore.class) == null) {
            return describeRepeatTest(method);
        }
        return super.describeChild(method);
    }

    private Description describeRepeatTest(FrameworkMethod method) {
        int times = method.getAnnotation(Repeat.class).value();

        Description description = Description.createSuiteDescription(
                testName(method) + " [" + times + " times]",
                method.getAnnotations());

        for (int i = 1; i <= times; i++) {
            Description d = Description.createSuiteDescription("[" + i + "] " + testName(method));
            d.addChild(Description.createTestDescription(getTestClass().getJavaClass(), testName(method)));
            description.addChild(d);
        }
        return description;
    }

    @Override
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {

        if (method.getAnnotation(Repeat.class) != null) {

            if (method.getAnnotation(Ignore.class) == null) {
                Description description = describeRepeatTest(method);
                runRepeatedly(methodBlock(method), description, notifier);
            }
            return;
        }

        super.runChild(method, notifier);
    }

    private void runRepeatedly(Statement statement, Description description,
                               RunNotifier notifier) {
        for (Description desc : description.getChildren()) {
            runLeaf(statement, desc, notifier);
        }
    }

}

最后,运行wat() 10 次的测试:

@RunWith(ExtendedRunner.class)
public class RepeatTest {

    private int counter = 0;

    @Repeat(10)
    @Test
    public void wat() {
        System.out.println(counter++);
    }
}

当我运行测试时,我在事件日志中得到以下信息。

Failed to start: 9 passed, 1 not started

还有这个:

有趣的是,测试从 0 打印到 9,这让我相信测试实际上运行了 10 次。然而,我得到了上面的事件日志。为什么会这样?

【问题讨论】:

    标签: java junit


    【解决方案1】:

    我在 GitHub 上发布了一个 @Rule 来重复测试。此解决方案比使用Runner 更灵活,因为它不排除使用另一个Runner

    RepeatTest Rule

    RepeatFailingTest Rule

    @ThreadSafe
    @ParametersAreNonnullByDefault
    public class RepeatTest implements TestRule {
    
    public static RepeatTest findRepeats() {
        return new RepeatTest();
    }
    
    private RepeatTest() {
        super();
    }
    
    @Override
    public Statement apply(final Statement statement, Description description) {
        return new StatementRepeater(statement, description);
    }
    
    private final class StatementRepeater extends Statement {
    
        private final Statement statement;
        private final int repeatCount;
    
        private StatementRepeater(Statement statement, Description description) {
            super();
            this.statement = statement;
            Repeat repeat = description.getAnnotation(Repeat.class);
            this.repeatCount = getRepeatCount(repeat);
        }
    
        private final int getRepeatCount(@Nullable Repeat repeat) {
            int count = 1;
            if (repeat != null) {
                count = repeat.count();
            }
    
            assertThat("Repeat count must be > 0", count,
                    OrderingComparison.greaterThan(0));
            return count;
        }
    
        @Override
        public void evaluate() throws Throwable {
            for (int i = 0; i < repeatCount; i++) {
                statement.evaluate();
            }
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 2019-10-20
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多