Last active
August 22, 2016 11:06
-
-
Save rciovati/8297685 to your computer and use it in GitHub Desktop.
Running small or medium tests with connectedInstrumentTest task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
} | |
apply plugin: 'android' | |
repositories { | |
mavenCentral() | |
} | |
android { | |
//All other configurations | |
defaultConfig { | |
versionCode 110 | |
versionName "1.1.0" | |
minSdkVersion 8 | |
targetSdkVersion 17 | |
testPackageName "it.mypackage.test" | |
testInstrumentationRunner getInstrumentationTestRunner(); | |
} | |
} | |
def getInstrumentationTestRunner() { | |
def result = "android.test.InstrumentationTestRunner"; | |
if (project.hasProperty("testSize")) { | |
switch (project.getProperties().get("testSize")) { | |
case "small": | |
result = "it.mypackage.test.runner.SmallTestsIntrumentationTestRunner"; | |
break; | |
case "medium": | |
result = "it.mypackage.test.runner.MediumTestsIntrumentationTestRunner"; | |
break | |
} | |
} | |
logger.lifecycle("Using TestRunner: " + result) | |
return result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.mypackage.test.runner; | |
import android.os.Bundle; | |
import android.test.InstrumentationTestRunner; | |
public class MediumTestsIntrumentationTestRunner extends InstrumentationTestRunner { | |
@Override | |
public void onCreate(Bundle arguments) { | |
arguments.putString("size", "medium"); | |
super.onCreate(arguments); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.mypackage.test.runner; | |
import android.os.Bundle; | |
import android.test.InstrumentationTestRunner; | |
public class SmallTestsIntrumentationTestRunner extends InstrumentationTestRunner { | |
@Override | |
public void onCreate(Bundle arguments) { | |
arguments.putString("size", "small"); | |
super.onCreate(arguments); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For running small tests: | |
`./gradlew connectedAndroidTest -PtestSize=small` | |
For running medium tests: | |
`./gradlew connectedAndroidTest -PtestSize=medium` | |
without the `testSize` property test will run with the default `android.test.InstrumentationTestRunner` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I stumbled across this and thought that it is worth mentioning that arguments can now be passed straight to the instrumentation runner. This feature has been available since version 1.3 of the android gradle plugin.
The android gradle DSL reference is here:
http://google.github.io/android-gradle-dsl/1.3/com.android.build.gradle.internal.dsl.ProductFlavor.html#com.android.build.gradle.internal.dsl.ProductFlavor:testInstrumentationRunnerArguments
You can now pass a size argument, at the CLI or by defining a function that determines the value of the argument to use.
One problem that I have found is that it is only available via the gradle plugin and cannot be done in Android Studio 1.4.1. You can't pass arguments to the instrumentation runner in the GUI. There is currently a issue raised for this, which has been re-assigned for Android Studio 1.5 (https://code.google.com/p/android/issues/detail?id=158537). Until then the approach of using custom instrumentation runners to run different size tests seems like a valid one.