AndroidJUnitRunner单元测试的执行速度非常慢

时间:2018-07-06 19:03:38

标签: android android-studio android-gradle android-testing android-junit

我在项目中使用的是AndroidJUnitRunner,但是单元测试在Android Studio中以及通过gradlew从命令行在设备和仿真器上执行的过程非常缓慢。

我正在使用此开源项目OneBusAway(截至this commit起)的主分支: 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'
...
}

为什么正在运行的单元测试这么慢?

1 个答案:

答案 0 :(得分:3)

显然,速度下降与包含对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'

这是新的build.gradle,它可以在约15秒内执行测试:

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依赖性。

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

相关问题