20

Using Android studio with gradle wrapper version 2.2.1, I am trying to run all the tests in one single test class, as well as a specific test inside that class and have tried using:

./gradlew  test --tests DownloadsActivityTest

like the documentation suggests, as well as

-DandroidTest.single=DownloadsActivityTest

But neither of these versions work.

How do I run a single test class, and a single test from the command line using the gradle wrapper?

 ./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
1
  • It's not clear if you are running local or instrumentation tests. If you are talking about instrumentation tests then this may be a duplicate of stackoverflow.com/q/19565857/1650674 Commented Feb 6, 2020 at 0:12

4 Answers 4

17

To only run instrumentation tests (androidTests) in a specific test class, execute:

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

To only run local tests ("unit", JVM tests) in a specific test class or package execute:

./gradlew :app:testDebugUnitTest --tests "com.example.android.testing.blueprint.unit.integrationTests.*"
Sign up to request clarification or add additional context in comments.

Comments

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

It is important to specify the module name where the test lives, otherwise, when gradle builds dependent modules, it will attempt to test them, and might fail because none of the tests fit the test pattern:

No tests found for given includes: [FooTest]

Also, unless you specify the fully-qualified-name (FQN) of the test, your test name pattern should start with a *.

In my case, I have a module named app and a variant named localDebug, and I wanted to test FooTest, so I ran:

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

1 Comment

This works for unit tests, not instrumentation tests.
2

This is supported in Android Studio 1.1, using the Android Gradle plugin v1.1.0.

Follow the guide here

1 Comment

he's trying to run a single test class. which the android studio gradle plugin does not support that's the problem
1

The Android Gradle plugin, as of 1.0.0, doesn't have support for running single Android tests. The feature request for it is filed at https://code.google.com/p/android/issues/detail?id=74196.

I know that better test support in general is very high on the post-1.0 priority list, but I can't say with any certainty when this will be implemented.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.