Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rciovati
Last active August 22, 2016 11:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rciovati/8297685 to your computer and use it in GitHub Desktop.
Save rciovati/8297685 to your computer and use it in GitHub Desktop.
Running small or medium tests with connectedInstrumentTest task
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
}
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);
}
}
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);
}
}
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`
@stephanenicolas
Copy link

That's a pretty hack riccardo ;)

@carlosRamirezLizan
Copy link

Hi Riccardo,

Thank you so much for the post. I was able to label my tests and run them separately.
Only one question: I have like 10 product flavors and the project is quite big, so I was wondering if
I could customize the command connectedAndroidTest so it only run test in one product flavor.

Thanks in advance!

@carlosRamirezLizan
Copy link

Ok I solved it, just type
gradlew.bat connectedAndroidTestFlavor1Debug -PtestSize=small
and this only runs your test in one product flavor.

@zebulon988
Copy link

Thanks,helpful.

@binarycreations
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment