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.
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.
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.
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.
Using android.test.InstrumentationTestRunner
no, this is not possible. You do, however, have options:
android.test.InstrumentationTestRunner
buildConfigField 'String', 'TEST_NAME', '${testName}'
, where testName
is '"${project.properties.get("test")}"'
if set, otherwise nulltestInstrumentationRunner
with your custom runner./gradlew connectedCheck -Ptest=com.example.Test.testSomething
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.
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
andtestReleas
e etc. If you want to run only some tests, using thegradle --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.
As answered in https://stackoverflow.com/a/32603798 there is now
android.testInstrumentationRunnerArguments.class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With