在Gradle中的每个构建之前运行单元测试 [英] Running unit tests before each build in Gradle

查看:286
本文介绍了在Gradle中的每个构建之前运行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android项目中配置 build.gradle 以在每次调试或发布构建之前运行所有单元测试?我知道我可以使用 dependsOn 设置任务依赖关系,但是我怎样才能为单元测试任务指定它?我想为我的项目的每个模块(Android和普通Java)执行此操作,是否有可能?

How can I configure a build.gradle in an Android project to run all my unit tests before each debug or release build? I know that I can set tasks dependencies with dependsOn, but how can I specify it for the unit test task? I'd like to do this for each (Android and plain Java) module of my project, is it possible?

推荐答案

你有一个特殊的任务只运行单元测试?或者你可以自由运行它作为简单的 test (或者更一般的 testDebug testRelease )?假设您每次调用 assembleDebug 时都要运行 testDebug testRelease c $ c>或 assembleRelease 任务。然后,您可以使用 dependsOn 任务属性。例如这样:

Do you have a special task to run only unit tests? Or you are free to run it as simple test (or more generally testDebug and testRelease)? Let's say, you want to run testDebug or testRelease every time you call assembleDebug or assembleRelease task. Then you can, as you've noted, use dependsOn task property. For example this way:

assembleDebug.dependsOn testDebug
assembleRelease.dependsOn testRelease

必须将此配置添加到您需要的每个build.gradle脚本(在项目的每个模块中)。如果您有多个测试任务,您可以通过这种方式设置任务依赖关系:

This configuration must be added to every build.gradle script (in every module of the project), where you need it. If you have a number of test tasksm you can set task dependencies this way:

tasks.assembleRelease.dependsOn {
    project.tasks.findAll { task ->
        task.name.startsWith('testRelease')
    }
}

当然,您可以尝试使用 allprojects 子项目来在root build.gradle脚本的根目录中设置此依赖项$ c>(你可以阅读这里),但您必须在根脚本中应用 android 插件,否则将不会找到任何任务。

Sure, you can try to set this dependencies in the root the root build.gradle script, by using allprojects or subprojects (you can read about it here), but you have to apply android plugin in the root script too, otherwise tasks won't be found.

这篇关于在Gradle中的每个构建之前运行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆