Gradle: How to run instrumentation test for class

2019-02-06 02:41发布

问题:

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

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

回答1:

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


回答2:

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