【问题标题】:Use Gradle test-retry plugin in custom test task definition在自定义测试任务定义中使用 Gradle 测试重试插件
【发布时间】:2020-06-11 08:40:13
【问题描述】:

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

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中描述的自定义规则成功运行测试

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

【问题讨论】:

    标签: java gradle junit


    【解决方案1】:
    task retryTest(type: Test) {
      description = 'Dummy Retry Test'
      group = 'verification'
      maxHeapSize = '2048m'
      include '**/*SpecificIntegrationTest.class'
      reports {
        junitXml {
          mergeReruns = true
        }
      }
    
      retry {
        maxRetries = 3
      }
    }
    

    【讨论】:

    • 请考虑在您的代码中添加一些解释,以便 OP 和未来的读者能够更好地理解其目的。
    • 社区鼓励在代码中添加解释,而不是纯粹基于代码的答案(参见here)。
    • 这是一个gradle任务,用于在诉讼结束时重新运行测试。如果不加“mergeReruns = true”,如果测试失败,结束成功,会报失败,因为junit解析器。添加这一点,junit xml reader 将获取 xml 报告中的最后一条记录,您的构建将成功。 retry {} 标签是在 gradle 5.0 中引入的,它实际上用于重新运行测试。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 2017-06-16
      相关资源
      最近更新 更多