如何对gradle任务进行单元测试?

时间:2018-03-29 14:53:05

标签: unit-testing gradle gradle-task

我想测试build.gradle脚本的逻辑 该剧本的摘录如下:

(...other tasks and methods...)
def readCustomerFile(File file) {
    def schema = <prepare schema>
    def report = schema.validate(JsonLoader.fromFile(file))
    if (!report.success) {
        throw new GradleException("File is not valid! " + report.toString())
    }
    return new groovy.json.JsonSlurper().parse(file)
}

task readFiles {
    mustRunAfter 'prepareCustomerProject'
    doLast {
        if (System.env.CUSTOMER_FILE_OVERRIDE) {
            project.ext.customerFileData = readCustomerFile(System.env.CUSTOMER_FILE_OVERRIDE)
        }
        else if (customerFile.exists()) {
            project.ext.customerFileData = readCustomerFile(customerFile)
        }
        else {
            throw new GradleException("Customer File is not provided! It is expected to be in CUSTOMER_FILE_OVERRIDE variable or in ${customerFile}")
        }
    }
}
(...other tasks and methods...)

我想测试方法和任务本身 'prepareProject'任务在执行时非常冗长,但在'真实'设置中,它不仅需要设置必要的属性,而且不仅仅是上面的任务。
对于测试我只想要例如设置运行readFiles任务并验证结果,确保项目中的任何属性都已正确设置或抛出异常。

我已经查看了gradle测试工具包,但这不是我需要的,因为我无法找到任何可以让我使用的内容。检查项目。
我见过Guide for Testing Gradle Scripts,但这篇文章已经很老了,并没有解决我的需求/问题。我也看了gradle docs Testing Build Logic with TestKit,但是看GradleRunner似乎没有提供任何真正的检查或项目准备能力。
此外,它将使我们使用jUnit,有效地添加整个类结构仅用于测试目的。不干净,难以维护。 谷歌搜索gradle +测试+任务和其他变化找到了很多运行xUnit测试的方法,但这不是我需要的。

总结一下,我需要的是:

  • 从build.gradle中分离测试gradle任务和方法(测试工具包将运行任务及其所有依赖项,我不希望这样)
  • 在测试运行之前准备项目(测试工具似乎不允许这样做)
  • 验证任务/方法输出

有没有人成功完成此任务?
或者我是以错误的方式接近这个? 我很擅长gradle,寻找测试我的构建脚本的好选项。

0 个答案:

没有答案
相关问题