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:

  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

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

Share:
11,126
Javier Manzano
Author by

Javier Manzano

JavaScript, Node.js, React, Blockchain, ...

Updated on June 25, 2022

Comments

  • Javier Manzano
    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
    IgorGanapolsky almost 9 years
    What about for jvm-based tests?
  • Shigerello
    Shigerello over 8 years
    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?
  • sschuberth
    sschuberth over 8 years
    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 tools.android.com/tech-docs/unit-testing-support.
  • mdelolmo
    mdelolmo over 8 years
    This is the best answer, considering the current circumstances
  • Peter Tran
    Peter Tran over 8 years
    For my instrumentation test I had to replace 'runner_class' parameter with com.example.test/android.test.InstrumentationTestRunner
  • Wahib Ul Haq
    Wahib Ul Haq about 7 years
    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 <modulename>:connectedAndroidTest
  • Tim Kist
    Tim Kist about 7 years
    Definitely simpler than accepted answer! See google.github.io/android-gradle-dsl/current/… for more info.
  • eastwater
    eastwater over 6 years
    It will run all the test methods in the class. the part #methodName is ignored.
  • Jameson
    Jameson about 4 years
    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.