testInstrumentationRunner

testInstrumentationRunner

本文介绍了如何使用Gradle动态切换/更改testInstrumentationRunner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有2个不同的测试组.一组仅使用默认AndroidJUnitRunner运行,另一组必须使用自定义实现TestRunner extends MonitoringInstrumentation运行.

My project has 2 different groups of tests. One group runs only with the default AndroidJUnitRunner the other has to be run with a custom implementation TestRunner extends MonitoringInstrumentation.

当前,每次需要运行另一组测试时,我都通过编辑build.gradle来切换testInstrumentationRunner:

Currently I switch the testInstrumentationRunner by editing the build.gradle each time I need to run the other group of tests:

android{
      defaultConfig {
          //testInstrumentationRunner "my.custom.TestRunner"
           testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
}

我知道口味可以有自己的testInstrumentationRunner,但是我当前的应用程序已经有2个flavourDimensions.实际上,使用口味旨在具有不同版本的应用程序.我需要2个版本的测试应用程序,两个版本都使用不同的testInstrumentationRunner测试同一应用程序.

I know that flavours can have their own testInstrumentationRunner but my current app already has 2 flavourDimensions. Using flavours is actually intended to have different versions of an app. I need 2 versions of the test application, both testing the same app with different testInstrumentationRunners.

我尝试通过遍历所有测试变体来更改testInstrumentationRunner.实际上有多个testInstrumentationRunner属性:

I tried to change the testInstrumentationRunner by iterating over all test variants. There are actually multiple testInstrumentationRunner properties:

android.testVariants.all { TestVariant variant ->
    //readonly
    variant.variantData.variantConfiguration.instrumentationRunner

    variant.variantData.variantConfiguration.defaultConfig.testInstrumentationRunner

}

但是,一旦调用android.testVariants,就对构建进行配置,并且所有更改都不会反映在构建中.

But as soon as android.testVariants is called the build gets configured and all changes are not reflected in the build.

如何动态更改testInstrumentationRunner(通过gradle插件)?

How can I change the testInstrumentationRunner (from a gradle plugin) dynamically?

我希望有2个不同的gradle任务,每个任务使用不同的testInstrumentationRunner,但是测试相同的变体.因为我打算创建gradle插件,所以该解决方案也应该可以作为插件使用.

I'd prefer to have 2 different gradle tasks, each using a different testInstrumentationRunner but testing the same variant. Because I intent to create a gradle plugin the solution should work as plugin too.

推荐答案

由于android gradle插件1.3可以创建单独的测试模块.每个测试模块都可以有自己的testInstrumentationRunner.

Since the android gradle plugin 1.3 it is possible to create separate test modules. Each of those test modules can have its own testInstrumentationRunner.

有关详细示例,请参见 AndroidTestingBlueprint 示例项目github.

For a detailed example see the AndroidTestingBlueprint example project on github.

来自@ johan-stuyts的解决方案获得了赏金,效果很好(或者至少它与android gradle插件1.2兼容).但是它使用私有API,并且创建单独的模块更加容易,并且可以面向未来.

The solution from @johan-stuyts that got bounty works fine (or at least it did with the android gradle plugin 1.2). But it uses private APIs and creating a separate module is easier and future proof.

这篇关于如何使用Gradle动态切换/更改testInstrumentationRunner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:55