我在我的项目中使用了 AndroidJUnitRunner,但是单元测试在 Android Studio 和命令行中通过gradlew
.
我正在使用这个开源项目的主分支 OneBusAway(截至本次提交): https ://github.com/OneBusAway/onebusaway-android
我看到所有 142 个测试的执行时间在实际经过的时间内超过 3 分钟,但 Android 仅在“测试结果”下显示的执行时间中注册了其中的一小部分。在切换到 AndroidJUnitRunner 之前,所有这些单元测试都在 20 秒内持续执行。
这是一个示例测试的样子:
/**
* 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);
}
}
这是build.gradle
配置:
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'
...
}
为什么这个运行单元测试这么慢?