简体   繁体   English

如何强制在Gradle Android构建上运行单元测试?

[英]How can I enforce that unit tests are run on Gradle Android build?

Hello fine android devs! 您好,优秀的Android开发者!

(I understand that this is a very basic question. I'm still wrapping my head around Gradle and Android, and my Googling on this particular one turns up nothing.) (我知道这是一个非常基本的问题。我仍在围绕Gradle和Android进行研究,而我在该特定谷歌上搜索完全没有结果。)

I have my first unit test written, and it runs fine using 我已经编写了我的第一个单元测试,并且使用

./gradlew test

I'm not exactly sure what is making it run since the test directory (src/test/java/tld/mycompany/myapp/MyTest.java) is not in build.gradle or app/build.gradle, but I'm assuming that it is performed by magic in the Android Gradle plugin. 我不确定是什么使它运行,因为测试目录(src / test / java / tld / mycompany / myapp / MyTest.java)不在build.gradle或app / build.gradle中,但是我假设它是由Android Gradle插件中的魔术执行的。 Either way, when I choose "Rebuild Project", the tests are not included in the build - if I intentionally break on of the tests, the build still succeeds. 无论哪种方式,当我选择“ Rebuild Project”时,构建中都不会包含测试-如果我有意中断测试,则构建仍会成功。

How do I make the build run the tests and fail if the tests fail? 如何使构建运行测试并在测试失败的情况下失败?

"Rebuild Project" just downloads the depenencies and compiles the code. “重建项目”仅下载依赖关系并编译代码。 If you want to build the project properly, use 如果要正确构建项目,请使用

./gradlew build

The reason the tests are run even if you don't declare them anywhere is, as you said, part of the android gradle plugin. 正如您所说,即使您未在任何地方声明它们也要运行测试,这是android gradle插件的一部分。 The tests in /test/ and /androidTest/ are both executed because of predefined build types. / test /和/ androidTest /中的测试都是由于预定义的构建类型而执行的。 With ./gradlew build, ALL present build types will be executed. 使用./gradlew构建,将执行所有当前的构建类型。

If you look here , you'll see the relationship between the gradle tasks for java (which is what it's using in Android). 如果您在此处查看 ,您将看到java的gradle任务之间的关系(这是Android中使用的)。

As tknell has said, both test and androidTest are executed because they're 'marked' as test directories in the imported android plugin. 正如tknell所说,test和androidTest都被执行,因为它们在导入的android插件中被“标记”为测试目录。

What the 'rebuild project' task does is look at the build.gradle file and see which dependencies and such are declared. “重建项目”任务的作用是查看build.gradle文件,并查看声明了哪些依存关系。 Based on that, it builds its project for Android Studio. 基于此,它为Android Studio构建其项目。 This (as far as I know) doesn't run any real gradle tasks. 据我所知,这没有运行任何真正的gradle任务。

You can not "Rebuild Project" with running your unit test. 您不能通过运行单元测试来“重建项目”。 But you can create your custom run for checking unit tests. 但是您可以创建自定义运行以检查单元测试。 By Run > Edit Configuration . 通过Run > Edit Configuration You can see more here. 您可以在这里看到更多。 http://rexstjohn.com/unit-testing-with-android-studio/ http://rexstjohn.com/unit-testing-with-android-studio/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Gradle运行一组特定的Android检测测试? - How can I run a specific set of Android instrumentation tests with Gradle? 如何使用IDEA + Gradle + Android运行单元测试 - How to run unit tests with IDEA+Gradle+Android Android Gradle:如何分别运行集成和单元测试? - Android gradle: how to run separately integration and unit tests? 如何防止Android Gradle构建将设备测试作为单元测试运行 - How to prevent Android gradle build from running device tests as unit-tests 在 Jenkins (Gradle) 上运行 android 单元测试和仪器测试 - Run android unit tests & instrumentation tests on Jenkins (Gradle) 如何为Android / Gradle设置单元测试 - How to set up unit tests for Android / Gradle 如何在Android Studio中构建本地/主机OS C ++单元测试? - How can I build local/host OS C++ unit tests in Android Studio? 依赖于构建工具0.11的“ aar”的Gradle Android单元测试。+ - Gradle Android unit tests that depend on an 'aar' for build tool 0.11.+ 如何在不同时启动应用程序的情况下运行Android单元测试? - How can I run Android unit tests while not simultaneously starting the application? 无法在Android Studio中运行单元测试 - Can't run unit tests in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM