如何使用gradle wrapper 2.2.1从命令行运行单个单元测试

时间:2014-12-18 21:43:53

标签: gradle android-gradle

使用带有gradle包装器版本2.2.1的Android工作室,我试图在一个单独的测试类中运行所有测试,以及在该类中进行特定测试并尝试使用:

./gradlew  test --tests DownloadsActivityTest

就像文档建议的那样,以及

-DandroidTest.single=DownloadsActivityTest

但这两个版本都不起作用。

如何使用gradle包装器从命令行运行单个测试类和单个测试?

 ./gradlew --version

------------------------------------------------------------
Gradle 2.2.1
------------------------------------------------------------

Build time:   2014-11-24 09:45:35 UTC
Build number: none
Revision:     6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a

Groovy:       2.3.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.7.0_51 (Oracle Corporation 24.51-b03)
OS:           Linux 3.17.6-200.fc20.x86_64 amd64

[16:33][j@localhost:~/myHomeDir]$ ./gradlew  test --tests DownloadsActivityTest
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for debugTest as it may be conflicting with the internal version provided by Android.
     In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for debugTest as it may be conflicting with the internal version provided by Android.
     In case of problem, please repackage it with jarjar to change the class packages

FAILURE: Build failed with an exception.

* What went wrong:
Problem configuring task :app:test from command line.
> Unknown command-line option '--tests'.

* Try:
Run gradlew help --task :app:test to get task usage details. Run with --stacktrace     option to get the stack trace. Run with --info or --debug                                                                                                                                  option to get more log output.

BUILD FAILED

Total time: 4.466 secs

4 个答案:

答案 0 :(得分:15)

要仅在特定测试类中运行 instrumentation 测试( androidTests ),请执行:

./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest

仅在特定测试类或包中执行本地测试(“单元”,JVM测试)执行:

./gradlew :app:testDebugUnitTest --tests "com.example.android.testing.blueprint.unit.integrationTests.*"

答案 1 :(得分:10)

./gradlew :<module name>:test<CapitalCasedBuildVariant> --tests "<Test name pattern>"

指定测试所在的模块名称非常重要,否则,当gradle构建依赖模块时,它将尝试测试它们,并且可能会失败,因为所有测试都不符合测试模式:

No tests found for given includes: [FooTest]

此外,除非您指定测试的完全限定名称(FQN),否则您的测试名称模式应以*开头。

就我而言,我有一个名为app的模块和一个名为localDebug的变体,我想测试FooTest,所以我跑了:

./gradlew :app:testLocalDebug --tests "*FooTest"

答案 2 :(得分:2)

Android Studio 1.1支持此功能,使用Android Gradle插件v1.1.0。

按照指南here

答案 3 :(得分:1)

从1.0.0开始,Android Gradle插件并不支持运行单个Android测试。其功能请求已在https://code.google.com/p/android/issues/detail?id=74196提交。

我知道在1.0后的优先级列表中,一般情况下,更好的测试支持非常高,但我无法确定何时会实现这一点。