我希望能够运行测试类指定的次数。该类看起来像:
@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:失败提供了有用的调试信息),因此我希望能够多次运行该类,并且最好以某种方式对结果进行分组。尝试过重复注释 http://codehowtos.blogspot.gr/2011/04/run-junit-test-repeatedly.html- 但这是特定于测试的(并且并没有真正使其发挥作用 - 见上文)并与重复测试类 http://etutorials.org/Programming/Java+extreme+programming/Chapter+4.+JUnit/4.10+Repeating+Tests/,它似乎无法转移到 Junit 4 - 我在 SO 上找到的最接近的是this https://stackoverflow.com/a/1835615/281545- 但显然它是 Junit3。在 Junit4 中,我的套件如下所示:
@RunWith(Suite.class)
@SuiteClasses({ TestSmithWaterman.class })
public class AllTests {}
我认为没有办法多次运行它。使用空选项进行参数化 https://stackoverflow.com/questions/1492856/easy-way-of-running-the-same-junit-test-over-and-over确实不是一个选择 - 因为无论如何我都需要我的参数
所以我在 Eclipse 中一次又一次地按下 Control + F11
Help
编辑(2017.01.25):有人继续并将其标记为问题的重复项,我明确表示其接受的答案不适用于此处
正如 @MatthewFarwell 在评论中所建议的,我实施了一个测试规则根据他的回答 https://stackoverflow.com/questions/8295100/how-to-re-run-failed-junit-tests-immediately/8301639#8301639
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 视图中可见。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)