【问题标题】:Running a single test several times with Gradle使用 Gradle 多次运行单个测试
【发布时间】:2013-12-02 10:06:07
【问题描述】:

我正在尝试将 Ant 构建脚本迁移到 Gradle 脚本,但我想知道:有没有让测试任务运行多次?

【问题讨论】:

  • 为什么要多次运行相同的测试?
  • 在某些情况下,运行 x 次后测试失败
  • 每次构建执行一个任务只能执行一次。恕我直言,您应该研究测试失败的原因。在我看来,它要么依赖于状态,要么依赖于某些外部服务。
  • 这是我们想要的功能,与特定测试无关。
  • 然后你应该编写一个测试来强制发生这种情况并验证最后一个失败。请记住,您应该支持测试隔离,因此让测试用例相互依赖是不好的,而是构建一个测试您需要的确切场景的测试用例。如果您能提供更多关于您的问题的详细信息,将会很有帮助。例如,为什么要运行它几次。如果您预计它会失败,为什么会失败,您试图通过这种情况测试或期望实现什么?

标签: testing junit gradle


【解决方案1】:

这可以通过继承 Test 任务类来轻松完成。

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
}

灵感来自 Rene Groeschke,https://gist.github.com/breskeby/836316

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 2013-08-06
    • 1970-01-01
    • 2018-06-23
    • 2018-02-03
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多