Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch/change testInstrumentationRunner dynamically with gradle

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.

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"
      }
}

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.

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

}

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

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

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.

like image 213
thaussma Avatar asked Sep 06 '25 09:09

thaussma


1 Answers

Have you considered using console parameter as a switch between two configurations? As simple as that:

android {
      defaultConfig {
           if (project.ext.has("customRunner")) {
               testInstrumentationRunner "my.custom.TestRunner"
           } else {
               testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
           }
      }
}

And then for example run gradlew aDeb -PcustomRunner if you want to test using custom runner or gradlew aDeb to use default.

I know it's not rocket science but simpler is better, right? You can use it in your plugin too, just obtain the Project object and do the similar thing.

like image 168
Dmide Avatar answered Sep 09 '25 01:09

Dmide