vosmith
vosmith

Reputation: 5658

Android gradle test framework: single class

Is it possible to run a single test class using the new Android gradle build framework?

I have a test package that has multiple test classes (All of them are InstrumentationTestCase classes). I've been able to setup my build.gradle file to run the test package

defaultConfig{
    testPackageName "com.company.product.tests"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"
}

But is there a way to test only one test case in that package? Otherwise I'll be using the age old adb shell am instrument -w .......

P.S. I don't have time right now to switch to Roboelectric, but I do see that its pretty much the defacto framework nowadays.

Upvotes: 8

Views: 1444

Answers (2)

friedger
friedger

Reputation: 645

As answered in https://stackoverflow.com/a/32603798 there is now android.testInstrumentationRunnerArguments.class

Upvotes: 3

Mike Holler
Mike Holler

Reputation: 975

Using android.test.InstrumentationTestRunner no, this is not possible. You do, however, have options:

Custom Test Runner

  1. Extend android.test.InstrumentationTestRunner
  2. Add a buildConfigField 'String', 'TEST_NAME', '${testName}', where testName is '"${project.properties.get("test")}"' if set, otherwise null
  3. In your runner, only run tests that match BuildConfig.TEST_NAME (if null, run all tests)
  4. Replace the testInstrumentationRunner with your custom runner
  5. 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 testRelease 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.

Upvotes: 4

Related Questions