【问题标题】:Junit4 run a test class a fixed number of times and display results (eclipse)Junit4运行一个测试类固定次数并显示结果(eclipse)
【发布时间】:2013-01-03 07:01:49
【问题描述】:

我希望能够运行指定次数的测试类。这个类看起来像:

@RunWith(Parameterized.class)
public class TestSmithWaterman {

    private static String[] args;
    private static SmithWaterman sw;
    private Double[][] h;
    private String seq1aligned;

    @Parameters
    public static Collection<Object[]> configs() {
        // h and seq1aligned values 
    }

    public TestSmithWaterman(Double[][] h, String seq1aligned) {
        this.h = h;
        this.seq1aligned = seq1aligned;
    }

    @BeforeClass
    public static void init() {
        // run smith waterman once and for all
    }

    @Test
    @Repeat(value = 20) // does nothing
    // see http://codehowtos.blogspot.gr/2011/04/run-junit-test-repeatedly.html
    public void testCalculateMatrices() {
        assertEquals(h, sw.getH());
    }

    @Test
    public void testAlignSeq1() {
        assertEquals(seq1aligned, sw.getSeq1Aligned());
    }

    // etc
}

上面的任何测试都可能失败(并发错误 - EDIT :失败提供了有用的调试信息),所以我希望能够多次运行该类,并且最好以某种方式对结果进行分组。尝试了Repeat annotation - 但这是特定于测试的(并没有真正使它工作 - 见上文)并且与RepeatedTest.class 斗争,它似乎无法转移到Junit 4 - 我在SO上找到的最接近的是this - 但显然它是 Junit3。在 Junit4 中,我的套件看起来像:

@RunWith(Suite.class)
@SuiteClasses({ TestSmithWaterman.class })
public class AllTests {}

而且我认为没有办法多次运行它。 Parametrized with empty options 真的不是一个选择 - 因为我无论如何都需要我的参数

所以我在 Eclipse 中一次又一次地按 Control + F11

帮助

编辑(2017.01.25):有人继续并将其标记为问题的重复,我明确表示其接受的答案不适用于此处

【问题讨论】:

  • 如果您的测试可能失败,则说明您的测试或代码有问题。
  • @Andr:请就问题发表评论 - 我知道(显然!)我正在展示一个用例,为什么我需要多次运行 - 我的测试提供调试信息跨度>
  • 那我把你的问题搞错了。这个问题本身并没有给我提供一个有效的用例,这就是为什么我想说服你;-)
  • 更好 :-) 恐怕我没有解决方案...
  • 你看到我对如何立即重新运行失败的 JUnit 测试的回答了吗? stackoverflow.com/questions/8295100/…

标签: java eclipse junit junit4


【解决方案1】:

按照 @MatthewFarwell 在 cmets 中的建议,我实施了一条测试规则 as per his answer

public static class Retry implements TestRule {

    private final int retryCount;

    public Retry(int retryCount) {
        this.retryCount = retryCount;
    }

    @Override
    public Statement apply(final Statement base,
            final Description description) {
        return new Statement() {

            @Override
            @SuppressWarnings("synthetic-access")
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;
                int failuresCount = 0;
                for (int i = 0; i < retryCount; i++) {
                    try {
                        base.evaluate();
                    } catch (Throwable t) {
                        caughtThrowable = t;
                        System.err.println(description.getDisplayName()
                            + ": run " + (i + 1) + " failed:");
                        t.printStackTrace();
                        ++failuresCount;
                    }
                }
                if (caughtThrowable == null) return;
                throw new AssertionError(description.getDisplayName()
                        + ": failures " + failuresCount + " out of "
                        + retryCount + " tries. See last throwable as the cause.", caughtThrowable);
            }
        };
    }
}

作为我的测试类中的一个嵌套类 - 并添加了

@Rule
public Retry retry = new Retry(69);

在我在同一个类中的测试方法之前。

这确实起到了作用——它确实重复了 69 次测试——在某些异常的情况下,会抛出一个新的 AssertionError,其中包含一些统计信息以及作为原因的原始 Throwable 的单个消息。所以统计信息也将在 Eclipse 的 jUnit 视图中可见。

【讨论】:

  • AssertionError 由@mineralf 编辑 - 我自己没有测试过,但它应该与我编辑的答案中所述相同。
猜你喜欢
  • 2015-12-08
  • 2013-10-11
  • 2011-01-20
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2019-02-15
相关资源
最近更新 更多