如何使用 Gradle 命令在 Android 中运行单个测试类?

M.A*_*ali 9 android unit-testing android-gradle-plugin android-instrumentation android-junit

在我的 Android 应用程序中,我有几个测试类。如果我运行以下命令,./gradlew connectedAndroidTest它将运行 android test 文件夹中的所有测试用例并为所有测试类生成测试报告,但我需要测试特定的测试类并为我尝试使用以下命令的测试类生成报告:

./gradlew test --tests com.login.user.UserLoginTest
Run Code Online (Sandbox Code Playgroud)

上面的命令抛出以下异常

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. Run with --scan to get full insights.

* Get more help at https://help.gradle.org


My app gradle version is 3.3
Run Code Online (Sandbox Code Playgroud)

Jua*_*nez 17

如果您的项目中有多种风格,您可能会收到此错误。与其使用 just test,不如尝试一个特定的测试任务:

./gradlew testDebugUnitTest --tests com.login.user.UserLoginTest
Run Code Online (Sandbox Code Playgroud)

  • 试过3.6.0版本,不行。选项 --tests 无法识别。 (6认同)
  • 通常,您还需要指定测试所在的模块,例如,`app` 模块的`./gradlew :app:testDebugUnitTest --tests com.login.user.UserLoginTest`。 (4认同)
  • 这似乎不适用于仪器测试。 (2认同)