How can I run a single instrumentation test with Gradle Android
11,126
Solution 1
you can run the single android test in two steps:
./gradlew installDebugAndroidTest
-
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
Solution 2
Since Android Gradle plugin version 1.3.0 you can use
./gradlew -Pandroid.testInstrumentationRunnerArguments.class=your.package.LandingActivityTests connectedAndroidTest
Solution 3
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.
Solution 4
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
![Javier Manzano](https://i.stack.imgur.com/AT9l3.png?s=256&g=1)
Comments
-
Javier Manzano about 2 years
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?
-
IgorGanapolsky almost 9 yearsWhat about for jvm-based tests?
-
Shigerello over 8 yearsGradle 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?
-
sschuberth over 8 yearsThe 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 tools.android.com/tech-docs/unit-testing-support. -
mdelolmo over 8 yearsThis is the best answer, considering the current circumstances
-
Peter Tran over 8 yearsFor my instrumentation test I had to replace 'runner_class' parameter with com.example.test/android.test.InstrumentationTestRunner
-
Wahib Ul Haq about 7 yearsIt was not working for me because of
modules
but for others if you want to run a particular TestClass in a particularmodule
then you can use:$ ./gradlew -Pandroid.testInstrumentationRunnerArguments.class=com.blah.blah.MyActivityTest <modulename>:connectedAndroidTest
-
Tim Kist about 7 yearsDefinitely simpler than accepted answer! See google.github.io/android-gradle-dsl/current/… for more info.
-
eastwater over 6 yearsIt will run all the test methods in the class. the part #methodName is ignored.
-
Jameson about 4 yearsI 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.