Java 在自定义测试任务定义中使用Gradle测试重试插件

Java 在自定义测试任务定义中使用Gradle测试重试插件,java,gradle,junit,Java,Gradle,Junit,我有一个自定义任务定义来运行特定的测试文件,每个测试都有特殊的设置。 我的任务定义如下所示: task retryTest(type: Test) { description = 'Dummy Retry Test' group = 'verification' maxHeapSize = '2048m' include '**/*SpecificIntegrationTest.class' } plugins { id "org.gradle.test

我有一个自定义任务定义来运行特定的测试文件,每个测试都有特殊的设置。 我的任务定义如下所示:

task retryTest(type: Test) {
    description = 'Dummy Retry Test'
    group = 'verification'
    maxHeapSize = '2048m'
    include '**/*SpecificIntegrationTest.class'
}
plugins {
    id "org.gradle.test-retry" version "1.1.1"
}

task retryTest(type: Test) {
    description = 'Dummy Retry Test'
    group = 'verification'
    maxHeapSize = '2048m'
    include '**/*SpecificIntegrationTest.class'
    test {
        retry {
            maxRetries = 2
        }
    }
}
现在,此设置中的一些测试是不可靠的,我尝试再次运行它们,如下所示:

task retryTest(type: Test) {
    description = 'Dummy Retry Test'
    group = 'verification'
    maxHeapSize = '2048m'
    include '**/*SpecificIntegrationTest.class'
}
plugins {
    id "org.gradle.test-retry" version "1.1.1"
}

task retryTest(type: Test) {
    description = 'Dummy Retry Test'
    group = 'verification'
    maxHeapSize = '2048m'
    include '**/*SpecificIntegrationTest.class'
    test {
        retry {
            maxRetries = 2
        }
    }
}
我编写了一个测试类,第一次总是失败,第二次总是成功:

public class RetryTest {

    private int execCount = 0;

    @Test
    public void throwException() {
        if (execCount == 0) {
            execCount++;
            throw new NotImplementedException();
        }
    }
}
不幸的是,测试只执行一次,整个测试套件失败。我可以使用中所述的自定义规则成功运行测试


是否有办法将测试重试插件与自定义任务定义一起使用?

您的任务配置错误。应该是:

task retryTest(type: Test) {
    description = 'Dummy Retry Test'
    group = 'verification'
    maxHeapSize = '2048m'
    include '**/*SpecificIntegrationTest.class'
    retry {
        maxRetries = 2
    }
}

请考虑对你的代码添加一些解释,这样OP和未来的读者能够更好地理解它的目的。社区鼓励在代码中添加解释,而不是纯粹的基于代码的答案(参见)。如果您没有添加“mergerruns=true”,如果测试失败并且结束成功,那么由于junit解析器的原因,它将被报告为失败。添加这一点,JUnitXML阅读器将获取xml报告中的最后一条记录,您的构建将成功。retry{}标记是在gradle 5.0中引入的,它实际上用于重新运行测试。