Running a single test several times with Gradle

2019-03-01 16:02发布

问题:

I'm trying to migrate an Ant build script to a Gradle one and i was wondering: Is there anyway to have a test task run several times?

回答1:

This can be easikly done by subclassing the Test task class.

class StressTest extends Test {
    // can be overwritten from within the task call
    int times = 5
    public FileTree getCandidateClassFiles() {
        FileTree candidates = super.getCandidateClassFiles()
        for (int i = 1; i < times; i++) {
            candidates = candidates + super.getCandidateClassFiles()
        }
        return candidates
    }
}

task stressTest(type: StressTest) {
    // run test 10 times
    times = 10
}

Inspired by Rene Groeschke, https://gist.github.com/breskeby/836316