如何使用 Gradle 运行一组特定的 Android 检测测试?

Dan*_*n J 5 continuous-integration android android-studio android-gradle-plugin

我有多个测试包:

com.mypackage.blackbox    - Robotium UI tests
com.mypackage.integration - REST integration tests
com.mypackage.unit        - low level unit tests
Run Code Online (Sandbox Code Playgroud)

我们的服务器团队需要能够在每次推送时只运行集成测试(它们需要几分钟),然后每晚运行所有测试(黑盒 UI 测试需要超过 10 分钟)。

这个很好的答案通过重载现有的 JUnit 注释(如@SmallTest或),提供了一种稍微笨拙(但有效)的方法@LargeTest

Gradle 文档建议测试过滤器是这样做的方法,例如

./gradlew connectedAndroidTestDevDebug --tests com.mypackage.integration.*
Run Code Online (Sandbox Code Playgroud)

但是,这会失败并出现> Unknown command-line option '--tests'.错误(大概是因为 Android Gradle 插件不支持 vanilla Gradle 所做的一切?)。

同一份文件说,他们计划在未来支持这些替代方案:

  • 基于自定义注解的过滤(未来)
  • 基于测试层次的过滤;执行扩展ceratain基类的所有测试(未来)
  • 基于某些自定义运行时规则的过滤,例如系统属性的特定值或某些静态(未来)

有没有人知道一种干净的方法来让它现在工作?现在,我计划@MediumTest在所有集成测试扩展的基类上使用注释,但我希望能够指定特定的包。使用@MediumTest@LargeTest滥用这些注释,因为我的集成和黑盒测试都是根据指南进行的大型测试。

Dan*_*n J 1

萨姆的答案是最通用的答案。然而,最简单的解决方案可能是使用InstrumentationTestRunner-e package上的选项:

在 java 包中运行所有测试: adb shell am Instrument -w -e package com.android.foo.subpkg com.android.foo/android.test.InstrumentationTestRunner

您可以将此选项与 Square 的Spoon库结合使用,因为它允许您指定单个类,或用于 -e将选项传递给测试运行程序(例如选项package):

--class-name        Test class name to run (fully-qualified)

--method-name       Test method name to run (must also use --class-name)

--e                 Arguments to pass to the Instrumentation Runner. This can be used
                    multiple times for multiple entries. Usage: --e <NAME>=<VALUE>.
                    The supported arguments varies depending on which test runner 
                    you are using, e.g. see the API docs for AndroidJUnitRunner.
Run Code Online (Sandbox Code Playgroud)

根据记录,Shazam 的Fork有一个更强大的正则表达式选项:

android.test.classes=REGEX - comma separated regexes that specify a pattern for the classes/packages to run
Run Code Online (Sandbox Code Playgroud)