I'm using the AndroidJUnitRunner in my project, but the unit tests are painfully slow to execute on a device and emulator both in Android Studio and from the command-line via gradlew
.
I'm using the master branch of this open-source project, OneBusAway (as of this commit): https://github.com/OneBusAway/onebusaway-android
I'm seeing execution time of all 142 tests that is upwards of 3 minutes in real elapsed time, but Android only registers a smaller portion of this in the execution time it shows under "Test Results". Before switching to the AndroidJUnitRunner all of these unit tests executed under 20 seconds consistently.
Here's what an example test looks like:
/**
* Tests to evaluate utility methods related to math conversions
*/
@RunWith(AndroidJUnit4.class)
public class MathUtilTest {
@Test
public void testOrientationToDirection() {
// East
double direction = MathUtils.toDirection(0);
assertEquals(90.0, direction);
}
}
Here's the build.gradle
config:
android {
dexOptions {
preDexLibraries true
}
compileSdkVersion this.ext.compileSdkVersion
buildToolsVersion "27.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 93
versionName "2.3.8"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
...
testOptions {
execution 'ANDROID_TEST_ORCHESTRATOR'
unitTests.includeAndroidResources true
}
...
}
dependencies {
...
// Unit tests
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
...
}
Why is this running unit tests so slow?