Using android.test.InstrumentationTestRunner
no, this is not possible. You do, however, have options:
Custom Test Runner
- Extend
android.test.InstrumentationTestRunner
- Add a
buildConfigField 'String', 'TEST_NAME', '${testName}'
, where testName
is '"${project.properties.get("test")}"'
if set, otherwise null
- In your runner, only run tests that match BuildConfig.TEST_NAME (if null, run all tests)
- Replace the
testInstrumentationRunner
with your custom runner
- Run tests with
./gradlew connectedCheck -Ptest=com.example.Test.testSomething
Use Spoon
Spoon is an excellent test runner extension that, among other things (like beautiful multi-device test reports), lets you run individual tests. Since you're using gradle, I recommend the Spoon Gradle Plugin, which has an example of exactly what you want to do in its README.
Update: Run Individual Unit Tests with Android Gradle Plugin
With the addition of unit testing support, Android now supports running individul unit tests.
This is just an anchor task, actual test tasks are called testDebug
and testReleas
e etc. If you want to run only some tests, using the gradle --tests
flag, you can do it by running ./gradlew testDebug --tests='*.MyTestClass'
.
Source
Edit: I should also add that Spoon is a drop-in replacement for running tests. You will not have to modify anything but a few lines in your build script to use it.