36

I'm trying to run the tests with this line... but this launches all tests:

./gradlew -DconnectedAndroidTest.single=LandingActivityTests connectedAndroidTest

How can I launch a single test?

tir38
  • 9,810
  • 10
  • 64
  • 107
Javier Manzano
  • 4,761
  • 16
  • 56
  • 86
  • Does this answer your question? [Running a specific instrumentation unit test with Gradle](https://stackoverflow.com/questions/19565857/running-a-specific-instrumentation-unit-test-with-gradle) – tir38 Feb 06 '20 at 00:09

4 Answers4

52

Since Android Gradle plugin version 1.3.0 you can use

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=your.package.LandingActivityTests connectedAndroidTest
sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • Gradle has -D option to specify system properties, which are passed to JVM, and that makes me think JVM-based tests can be accomplished using only gradlew shell command. Somebody happens to know where to dig information for properties of instrumentation or unit-testing tests? – Shigerello Nov 11 '15 at 01:26
  • 1
    The OP is asking about `connectedAndroidTest`, i.e. tests than run on the device / emulator. Unit tests running in the JVM are a different topic. That said, you may want to have a look at the "Running from Gradle" section at http://tools.android.com/tech-docs/unit-testing-support. – sschuberth Nov 11 '15 at 07:49
  • 2
    This is the best answer, considering the **current** circumstances – mdelolmo Jan 31 '16 at 20:08
  • 1
    I wish this were the accepted answer. I come to this page all the time to copy-paste this line. It'd be easier if it were at the top of the page. – Jameson Apr 26 '20 at 23:11
22

you can run the single android test in two steps:

  1. ./gradlew installDebugAndroidTest
  2. adb shell am instrument -w -e class com.example.MyInstrumentationTest#testFoo com.example.test/android.support.test.runner.AndroidJUnitRunner
    https://developer.android.com/tools/testing/testing_otheride.html
aaashun
  • 316
  • 3
  • 8
  • 1
    For my instrumentation test I had to replace 'runner_class' parameter with com.example.test/android.test.InstrumentationTestRunner – Peter Tran Feb 04 '16 at 01:56
13

if you want to run just one test inside the class do something like

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=my.app.package.register.RegisterEmailTest#can_register connectedAndroidTest

can_register is a method in a class RegisterEmailTest

NOTE: the package needs to reference where the class is otherwise it will not work.

PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
  • 2
    It was not working for me because of `modules` but for others if you want to run a particular TestClass in a particular `module` then you can use: `$ ./gradlew -Pandroid.testInstrumentationRunnerArguments.class=com.blah.blah.MyActivityTest :connectedAndroidTest` – Wahib Ul Haq Apr 03 '17 at 09:53
  • Definitely simpler than accepted answer! See https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html#com.android.build.gradle.internal.dsl.ProductFlavor:testInstrumentationRunnerArguments for more info. – Tim Kist May 03 '17 at 11:08
  • It will run all the test methods in the class. the part #methodName is ignored. – eastwater Feb 25 '18 at 00:01
3

Visit Testing
Sadly, gradle connectedAndroidTest task is not supporting all the arguments.
There is feature request for gradle team.
If you are using Android Studio, you can create Run Configuration that launches specific test via adb shell am instrument

imort
  • 106
  • 5