Running different types of testInstrumentationRunner in different test tasks within the Android Gradle system
-
21-12-2019 - |
سؤال
I have some different test suites that I want to run from different Gradle tasks. Each one might have a different set or dependencies and different testInstrumentationRunner
. For example, I would like the following command line functionality:
gradle connectedAndroidTest unitTest
- uses dependencies from
androidTestCompile
andunitTestCompile
- runs tests in both the
/src/androidTest
and/src/unitTest
directories - uses the standard
testInstrumentationRunner
gradle connectedAndroidTest uiTest
- uses dependencies from
androidTestCompile
anduiTestCompile
- runs tests in both the
/src/androidTest
and/src/uiTest
directories - uses "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" as its
testInstrumentationRunner
Is this possible -- maybe with variants or flavors?
المحلول
I know of a few ways to do this, but this is the most straight forward. I recently accomplished this by using the small, medium, and large annotations provided by Google. Unit tests will typically be the small and medium tests while the Espresso tests will be the large tests (Google explains the logic behind this).
From here, you create a custom build config field and set it in your build.gradle file, like so:
defaultConfig {
....
buildConfigField "String", "TEST_SIZE", project.hasProperty('testSize') ? "\"${testSize}\"" : "null"
}
In your custom test runner, you can handle any last second adjustments before running the tests. If you grab the test value out of the build config and set it to the arguments before it is sent off, then only the tests with that annotation will run.
String sizeValue = BuildConfig.TEST_SIZE;
if(!TextUtils.isEmpty(sizeValue)){
arguments.putString("size", sizeValue);
}
From the command line, simply run:
./gradlew connectedAndroidTest -PtestSize=small
Also, to simplify your structure a bit, you can put all of your tests in the androidTest directory. Also, if you want to get specific on a particular folder, you can do this using the same logic above and just use a different argument. You just need to be aware of which argument you need to set it to. Don't worry that you're not running tests through shell; the arguments are the same.