Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 433
vosmith Avatar asked Dec 18 '13 15:12

vosmith


People also ask

How do I run a single test case in Gradle?

In versions of Gradle prior to 5, the test. single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest.

Does Gradle run tests in parallel?

One of the best features in Gradle for JVM-related projects is its ability to run tests in parallel. As discussed in the Gradle documentation, this implemented by setting the maxParallelForks property inside a test block in the build.

How do I create a test task in Gradle?

The test task automatically detects and executes all the unit tests in the test source set., Once the test execution is complete, it also generates a report. JUnit and TestNG are the supported APIs. The test task provides a Test. getDebug() method that can be set to launch to make the JVM wait for a debugger.


2 Answers

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.

like image 199
Mike Holler Avatar answered Oct 25 '22 14:10

Mike Holler


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

like image 27
friedger Avatar answered Oct 25 '22 16:10

friedger