28

I'm running instrumentation test in Android Studio with Run Configuration defined as below (don't mind warning): enter image description here

So this is invoking test suit for a specific class. How can I achieve this with command line, I guess using ./gradlew command ?

rciovati
  • 27,603
  • 6
  • 82
  • 101
Adam Styrc
  • 1,517
  • 2
  • 23
  • 38

2 Answers2

67

As stated in the AndroidTestingBlueprint you can use the android.testInstrumentationRunnerArguments.class property:

./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest
rciovati
  • 27,603
  • 6
  • 82
  • 101
  • how to run it for a particular method? – Dinesh Aug 17 '17 at 08:41
  • 20
    `./gradlew app:connectedAndroidTest -P android.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest#someMethodToTest` – Tim Kist Nov 14 '17 at 15:41
1

According to the docs:

When you run tests from the command-line with Android Debug Bridge (adb), you get more options for choosing the tests to run than with any other method. You can select individual test methods, filter tests according to their annotation, or specify testing options. Since the test run is controlled entirely from a command-line, you can customize your testing with shell scripts in various ways.

To run instrumentation tests with adb for a particular class do:

adb shell am instrument -w -e class 'com.myapp.MyActivityTest' com.myapp.test/android.support.test.runner.AndroidJUnitRunner

Note that if you've defined a custom testInstrumentationRunner on your app/build.gradle file then you need to replace android.support.test.runner.AndroidJUnitRunner with your own, like this:

adb shell am instrument -w -e class 'com.myapp.MyActivityTest' com.myapp.test/com.myapp.MyCustomTestRunner

Tip: If you get an error because the command isn't right, know that you can simply get the right command by running the tests from within Android Studio. You'll see the command on the Run window output.


These 2 documentation pages contain execution options:

https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner#typical-usage

https://developer.android.com/studio/test/command-line#AMSyntax

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73