AndroidJUnitRunner 单元测试执行起来很慢

AndroidJUnitRunner unit tests are very slow to execute

我在我的项目中使用了 AndroidJUnitRunner,但是单元测试在 Android Studio 中和从命令行通过 gradlew.

我正在使用这个开源项目的主分支 OneBusAway(截至 this commit): https://github.com/OneBusAway/onebusaway-android

我看到所有 142 个测试的执行时间在实际运行时间中超过 3 分钟,但 Android 仅在 [=28= 下显示的执行时间中记录了其中的一小部分].在切换到 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'
...
}

为什么这个 运行 单元测试这么慢?

显然,减速与包含对 Android Test Orchestrator 的依赖有关,我不需要(即使我 运行 在需要 Android 的设备上进行测试语境)。从现有文档中我不清楚这些测试不需要 Orchestrator。

我从 build.gradle 中删除了以下几行,我通过 Android Studio 的总执行时间下降到 ~15 秒范围内:

// 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'
...
execution 'ANDROID_TEST_ORCHESTRATOR'
...
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'

所以这是在 ~15 秒内执行测试的新 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"
    }
    ...
    testOptions {
        unitTests.includeAndroidResources true
    }
    ...
}
...
dependencies {
    ...
    // Unit tests
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

我认为 testInstrumentationRunnerArguments clearPackageData: 'true' 可能是导致执行时间增加以清除包数据的罪魁祸首,但仅删除该行并没有改变测试的总执行时间 - 我不得不完全删除Orchestrator 依赖项。

这是在 Github 上删除 Orchestrator 的提交: https://github.com/OneBusAway/onebusaway-android/commit/a1657c443258ac49b1be83a75399cf2ced71080e