Testing 使用Gradle多次运行单个测试

Testing 使用Gradle多次运行单个测试,testing,junit,gradle,Testing,Junit,Gradle,我正在尝试将Ant构建脚本迁移到Gradle脚本,我想知道:是否有必要让测试任务运行几次?这可以通过对测试任务类进行子类化轻松完成 class StressTest extends Test { // can be overwritten from within the task call int times = 5 public FileTree getCandidateClassFiles() { FileTree candidates = super

我正在尝试将Ant构建脚本迁移到Gradle脚本,我想知道:是否有必要让测试任务运行几次?

这可以通过对测试任务类进行子类化轻松完成

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
}
类压力测试扩展了测试{
//可以从任务调用中覆盖
整数倍=5
公共文件树getCandidateClassFiles(){
FileTree候选文件=super.getCandidateClassFiles()
for(int i=1;i

受Rene Groeschke的启发,

想要多次运行相同测试的原因是什么?在某些情况下,一个测试在x次运行后失败,每次构建执行只能执行一次runsA任务。我想你应该调查一下为什么考试失败。在我看来,它似乎依赖于某个状态或某个外部服务。这是我们想要的功能,与特定的测试无关。然后,您应该编写一个测试,强制执行该测试,并验证最后一个测试是否失败。请记住,您应该支持测试隔离,因此让测试用例相互依赖是没有好处的,而不是构建一个测试您所需要的确切场景的测试用例。如果你能对你的问题提供更多的细节,那将是很有帮助的。例如,为什么要多次运行它。如果你认为它会失败,为什么会失败?你想用这样的案例来测试或期望实现什么?