1

I have a custom task definition to run specific test files with special settings per test. My task definition looks like this:

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

Now, some tests in this setup are flaky and I try do rerun them a second time like this:

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
        }
    }
}

I wrote a test class that always fails the first time, but succeeds the second time:

public class RetryTest {

    private int execCount = 0;

    @Test
    public void throwException() {
        if (execCount == 0) {
            execCount++;
            throw new NotImplementedException();
        }
    }
}

Unfortunately, the test is only executed once and the complete test suite fails. I can run the tests successfully by using a custom rule as described in https://stackoverflow.com/a/55178053/6059889

Is there some way to use the test-retry plugin with custom task definitions?

Steffen Schmitz
  • 860
  • 3
  • 16
  • 34

2 Answers2

2

Your task config is wrong. It should be:

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

  retry {
    maxRetries = 3
  }
}
Bozhidar Marinov
  • 116
  • 2
  • 15
  • 2
    Please consider adding some explanation to your code so OP and future readers are better able to understand the purpose. – mousetail Jan 25 '21 at 14:19
  • The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Jan 26 '21 at 05:11
  • It is gradle task which is used to rerun the test at the end of the suit. If you do not add "mergeReruns = true", if the test fails and the end succeeds, it will be reported as fails, because of the junit parsers. Adding this, junit xml reader will take the last record in the xml report and your build will be successful. retry {} tag is introduced in gradle 5.0 and it is actually used for reruning the tests. – Bozhidar Marinov Feb 01 '21 at 09:13