首页 > 解决方案 > 在自定义测试任务定义中使用 Gradle 测试重试插件

问题描述

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

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();
        }
    }
}

不幸的是,测试只执行一次,整个测试套件都失败了。我可以使用https://stackoverflow.com/a/55178053/6059889中描述的自定义规则成功运行测试

有什么方法可以将 test-retry 插件与自定义任务定义一起使用?

标签: javagradlejunit

解决方案


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

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

推荐阅读