首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AndroidJUnitRunner单元测试执行起来非常慢

AndroidJUnitRunner单元测试执行起来非常慢
EN

Stack Overflow用户
提问于 2018-07-07 03:03:38
回答 1查看 1.7K关注 0票数 6

我在我的项目中使用了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'
...
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 03:03:38

显然,速度减慢与包含对Android Test Orchestrator的依赖有关,这是我不需要的(即使我在需要安卓环境的设备上运行测试)。从现有的文档中我并不清楚这些测试是否不需要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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51216343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档