6

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

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

4

1 回答 1

4

显然,减速与包含对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 依赖项。

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

于 2018-07-06T19:03:38.333 回答