簡體   English   中英

JUnit重復注釋不會啟動測試用例

[英]JUnit repeat annotation does not start test case

我有一個@Repeat批注,可用於重復運行JUnit測試。 該代碼來自此博客文章並經過修改可與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次。 但是,我得到了上面的事件日志。 為什么會這樣?

我在GitHub上發布了一個@Rule ,可以重復測試。 該解決方案比使用Runner更靈活,因為它不排除使用其他Runner

重復測試規則

RepeatFailingTest規則

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM