Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a single instrumentation test with Gradle Android

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?

like image 913
Javier Manzano Avatar asked Jul 25 '14 08:07

Javier Manzano


People also ask

How do I run a specific test in gradle?

You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.

What is Android instrumentation testing?

Instrumented tests run on Android devices, whether physical or emulated. As such, they can take advantage of the Android framework APIs. Instrumented tests therefore provide more fidelity than local tests, though they run much more slowly.


4 Answers

Since Android Gradle plugin version 1.3.0 you can use

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=your.package.LandingActivityTests connectedAndroidTest
like image 69
sschuberth Avatar answered Oct 19 '22 16:10

sschuberth


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
like image 22
aaashun Avatar answered Oct 19 '22 15:10

aaashun


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.

like image 13
PedroAGSantos Avatar answered Oct 19 '22 15:10

PedroAGSantos


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

like image 3
imort Avatar answered Oct 19 '22 16:10

imort