Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反复运行同一个junit测试的简单方法?_Java_Eclipse_Junit - Fatal编程技术网

Java 反复运行同一个junit测试的简单方法?

Java 反复运行同一个junit测试的简单方法?,java,eclipse,junit,Java,Eclipse,Junit,正如标题所说,我正在寻找一些简单的方法来使用Eclipse自动连续多次运行JUnit4.x测试 例如,连续10次运行同一测试并报告结果 我们已经有了一个复杂的方法来做这件事,但我正在寻找一个简单的方法来做这件事,这样我可以有点确定我一直试图修复的脆弱测试保持不变 理想的解决方案是我不知道的Eclipse插件/设置/功能。我发现Spring的重复注释对这类事情很有用: @Repeat(value = 10) 最新(Spring Framework 4.3.11.API发布版)文档: 实现这

正如标题所说,我正在寻找一些简单的方法来使用Eclipse自动连续多次运行JUnit4.x测试

例如,连续10次运行同一测试并报告结果

我们已经有了一个复杂的方法来做这件事,但我正在寻找一个简单的方法来做这件事,这样我可以有点确定我一直试图修复的脆弱测试保持不变


理想的解决方案是我不知道的Eclipse插件/设置/功能。

我发现Spring的重复注释对这类事情很有用:

@Repeat(value = 10)
最新(Spring Framework 4.3.11.API发布版)文档:

实现这一点的最简单方法(所需的新代码最少)是将测试作为参数化测试运行(使用
@RunWith(Parameterized.class)
注释,并添加一个方法以提供10个空参数)。这样,框架将运行测试10次

该测试需要是类中唯一的测试,或者更好地说,所有测试方法都需要在类中运行10次

以下是一个例子:

@RunWith(Parameterized.class)
public class RunTenTimes {

    @Parameterized.Parameters
    public static Object[][] data() {
        return new Object[10][0];
    }

    public RunTenTimes() {
    }

    @Test
    public void runsTenTimes() {
        System.out.println("run");
    }
}
有了上面的内容,甚至可以使用无参数构造函数来实现,但我不确定框架作者是否有意这样做,或者这在将来是否会中断

如果您正在实现自己的runner,那么您可以让runner运行测试10次。如果您使用的是第三方运行程序,那么在4.7中,您可以使用新的
@Rule
注释并实现
MethodRule
接口,以便它接受语句并在for循环中执行10次。这种方法目前的缺点是,之前的
@和之后的
@只运行一次。这可能会在JUnit的下一个版本中发生变化(之前的
@规则将在
@规则之后运行),但无论如何,您将在对象的同一个实例上执行操作(对于
参数化的
运行程序,这是不正确的)。这假设运行类的任何运行程序都能正确识别
@Rule
注释。只有授权给JUnit跑步者时才是这样

如果您使用的自定义运行程序无法识别
@Rule
注释,那么您就必须编写自己的运行程序,将其适当地委托给该运行程序并运行10次

请注意,还有其他可能解决此问题的方法(如Theory runner),但它们都需要一个runner。不幸的是,JUnit目前不支持跑步者层。这是一个束缚其他跑步者的跑步者。

有什么问题吗:

@Test
void itWorks() {
    // stuff
}

@Test
void itWorksRepeatably() {
    for (int i = 0; i < 10; i++) {
        itWorks();
    }
}
@测试
无效工程(){
//东西
}
@试验
可重复地使其无效(){
对于(int i=0;i<10;i++){
它起作用();
}
}
与测试每个值数组的情况不同,您并不特别关心哪个运行失败


无需在配置或注释中执行您在代码中可以执行的操作。

库中有一个间歇注释,它与JUnit 4.7的
@规则一起工作,以重复多次测试或使用
@RunWith

比如说,

@RunWith(IntermittentTestRunner.class)
public class IntermittentTestRunnerTest {

   private static int testCounter = 0;

   @Test
   @Intermittent(repition = 99)
   public void annotatedTest() {
      testCounter++;
   }
}

运行测试后(在
@RunWith
中使用间歇性TestRunner),
测试计数器将等于99。

这对我来说更容易

public class RepeatTests extends TestCase {

    public static Test suite() {
        TestSuite suite = new TestSuite(RepeatTests.class.getName());

        for (int i = 0; i < 10; i++) {              
        suite.addTestSuite(YourTest.class);             
        }

        return suite;
    }
}
公共类RepeatTests扩展了TestCase{
公共静态测试套件(){
TestSuite=newTestSuite(RepeatTests.class.getName());
对于(int i=0;i<10;i++){
addTestSuite(YourTest.class);
}
返回套房;
}
}

灵感来自以下资源:

例子 创建并使用
@Repeat
注释,如下所示:

公共类MyTestClass{
@统治
public RepeatRule RepeatRule=新RepeatRule();
@试验
@重复(10)
公共空testMyCode(){
//你的测试代码在这里
}
}
Repeat.java
导入静态java.lang.annotation.ElementType.annotation\u类型;
导入静态java.lang.annotation.ElementType.METHOD;
导入java.lang.annotation.Retention;
导入java.lang.annotation.RetentionPolicy;
导入java.lang.annotation.Target;
@保留(RetentionPolicy.RUNTIME)
@目标({方法,注释类型})
公共@接口重复{
int value()默认值为1;
}
RepeatRule.java
import org.junit.rules.TestRule;
导入org.junit.runner.Description;
导入org.junit.runners.model.Statement;
公共类RepeatRule实现TestRule{
私有静态类RepeatStatement扩展语句{
非公开最终声明;
私人最终整数重复;
公共重复语句(语句语句,int repeat){
这个。声明=声明;
这个。重复=重复;
}
@凌驾
public void evaluate()可丢弃{
for(int i=0;i
PowerMock
将此解决方案与
@RunWith(PowerMockRunner.class)
一起使用,需要更新到(包括)。

我构建了一个允许执行此类测试的模块。但它不仅关注重复。但是为了保证某些代码是线程安全的

Maven依赖项:

<dependency>
    <groupId>org.lite</groupId>
    <artifactId>concurrent-testing</artifactId>
    <version>1.0.0</version>
</dependency>

这是一个开源项目。随时改进

对于JUnit 5,我能够使用注释解决这个问题:

@RepeatedTest(10)
public void testMyCode() {
    //your test code goes here
}
不是
@RepeatedTest(10)
public void testMyCode() {
    //your test code goes here
}
package tests;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.Result;

public class RepeatedTest {

    @Test
    public void test() {
        fail("Not yet implemented");
    }

    public static void main(String args[]) {

        boolean runForever = true;

        while (runForever) {
            Result result = org.junit.runner.JUnitCore.runClasses(RepeatedTest.class);

            if (result.getFailureCount() > 0) {
                runForever = false;
               //Do something with the result object

            }
        }

    }

}
@RunWith(Parameterized::class)
class MyTest {

    companion object {

        private const val numberOfTests = 200

        @JvmStatic
        @Parameterized.Parameters
        fun data(): Array<Array<Any?>> = Array(numberOfTests) { arrayOfNulls<Any?>(0) }
    }

    @Test
    fun testSomething() { }
}
    @RepeatedTest(3) // Runs N Times
    public void test_Prime() {
    }