【问题标题】:Run unittests just on one build variant with gradle使用 gradle 仅在一个构建变体上运行单元测试
【发布时间】:2019-01-17 10:03:37
【问题描述】:

我有一个多维 android gradle 项目,它需要很长时间才能构建和测试。 我有一个二维的flaver定义。 第一个维度有 2 个项目值,第二个维度有 4 个环境定义,并且有 3 种构建类型。

这会产生 2x4x3 = 24 个构建变体。 我想以某种方式进行优化,即只构建一个构建变体,并且只有一个构建变体用于在 ci 环境中运行单元测试。

build.gradle

android {
// more configurations
flavorDimensions "project", "environment"

productFlavors {
basic  {
    dimension "project"
}

advanced {
    dimension "project"
}

flavorDevelopment {
    dimension "environment"
    applicationId "ch.myproject.app.development"
}

flavorTest {
    dimension "environment"
    applicationId "ch.myproject.app.test"
}

flavorIntegration {
    dimension "environment"
    applicationId "ch.myproject.app.integration"
}

flavorProduction {
    dimension "environment"
    applicationId "ch.myproject.app.production"
}

buildTypes {
    debug {
        testCoverageEnabled = true
        minifyEnabled = false
        shrinkResources = false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }

    debugInstantRun.initWith(buildTypes.debug)
    debugInstantRun {
        // we are changing this build variant later on runtime, so that it will use constant values
        // for versionCode and versionName in the Manifest, for make sure the
        // manifest is unchanged between the instantRun builds

        // Specifies a sorted list of fallback build types that the
        // plugin should try to use when a dependency does not include a
        // "debugInstantRun" build type. You may specify as many fallbacks as you
        // like, and the plugin selects the first build type that's
        // available in the dependency.
        matchingFallbacks = ['debug']
    }

    release {
        // Currently all environments (dev/test/int/prod) are signed by the Production certificates
        minifyEnabled = false
        shrinkResources = false
    }
}
// more configurations
} // end of android

我以前会清理所有东西, gradlew clean --refresh-dependencies

然后只是组装调试变体, gradlew assembleDebug

然后我尝试在一个调试变体上只运行单元测试: gradlew testAdvancedFlavorDevelopmentDebugUnitTest -> 这不起作用

如果我运行gradlew test,所有构建变体都已构建(发布构建类型除外),测试正在运行,但这需要很长时间!

也试过gradlew testDebugUnitTest -> 不行

我想我可以将单元测试移动到另一个目录而不是测试,例如 testAdvancedFlavorDevelopment 然后当我输入 gradlew test 时,只会启动 testAdvancedFlavorDevelopmentDebug 和 testAdvancedFlavorDevelopmentDebugInstantRun 的测试。

但必须有一种方法让测试位于测试目录中,并通过 gradlew 命令强制编译和单元测试一个特定的构建变体!有什么想法吗?

卢克

【问题讨论】:

    标签: android unit-testing gradle build-variant


    【解决方案1】:

    try ./gradlew :ModuleName:testVariantNameDebugUnitTest,您还可以在 Gradle 选项卡中找到您可以执行的所有可能的 UnitTest 任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 2010-09-22
      相关资源
      最近更新 更多