java在Eclipse中,如何多次运行JUnit测试用例 1 年,5 月 Questions & Answers 9304 我有一个单元测试有时会失败,调试它是一件痛苦的事情,因为我不知道为什么它有时会失败 在Eclipse中有没有一种方法可以让我运行JUnit测试5次或50次 谢谢
# 1 楼答案 你试过这样的东西吗 @Test public void runMultipleTests() { for (int i = 0; i < 10; i++) { myTestMethod(); } }
# 2 楼答案 我刚刚找到了以下解决方案,它不需要任何额外的依赖性(你得到的答案之一需要Spring) 使用Parameterized运行程序运行测试: @RunWith(Parameterized.class) 然后添加以下方法,以提供与要运行测试的次数相等的多个空参数: @Parameterized.Parameters public static List<Object[]> data() { return Arrays.asList(new Object[10][0]); } 这样你甚至不用写循环。IntelliJ和eclipse还将每次迭代的结果分组在一起
# 3 楼答案 灵感来源于this solution: 使用@Repeat注释如下: public class MyTestClass { @Rule public RepeatRule repeatRule = new RepeatRule(); @Test @Repeat(10) public void testMyCode() { //your test code goes here } } 您只需要这两个类: 重复一遍。爪哇: 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; } 重复规则。爪哇: 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编辑: 为了在使用@RunWith(PowerMockRunner.class)时使用此解决方案,请更新到Powermock 1.6.5(,其中包括this patch)
# 4 楼答案 有一个测试装饰器。参见Junit API http://junit.org/apidocs/junit/extensions/RepeatedTest.html 比如 @Test @Repeat(10) public void FailRandomlyNeedToKnowWhy() { .... }
# 1 楼答案
你试过这样的东西吗
# 2 楼答案
我刚刚找到了以下解决方案,它不需要任何额外的依赖性(你得到的答案之一需要Spring)
使用
Parameterized
运行程序运行测试:然后添加以下方法,以提供与要运行测试的次数相等的多个空参数:
这样你甚至不用写循环。IntelliJ和eclipse还将每次迭代的结果分组在一起
# 3 楼答案
灵感来源于this solution:
使用@Repeat注释如下:
您只需要这两个类:
重复一遍。爪哇:
重复规则。爪哇:
2016-10-25编辑: 为了在使用
@RunWith(PowerMockRunner.class)
时使用此解决方案,请更新到Powermock 1.6.5(,其中包括this patch)# 4 楼答案
有一个测试装饰器。参见Junit API http://junit.org/apidocs/junit/extensions/RepeatedTest.html
比如