JCAndKSolutions / android-unit-test

Gradle plugin to add unit testing to android plugin. Prepared for Robolectric.
Other
343 stars 35 forks source link

Excluding build types from unit tests #6

Closed jrgonzalezg closed 10 years ago

jrgonzalezg commented 10 years ago

First of all, thank you very much for this great plugin which has allowed us to have a working testing setup for our Android projects.

That being said, i have a small feature request for you to consider. I have not seen any reference to a forum to discuss these topics, so i am commenting this here despite it is more a feature suggestion than an issue. My apologies if this is not the right place to do so.

The feature i am missing in the plugin is the ability to exclude specific buildTypes from unit tests. Right now, just the release build type is excluded, but all other buildTypes are included and they may be unsuitable for unit testing. For instance, in my projects i have an internalRelease build type, that inherits from the release build type and adds a few minor changes to be used internally by developers. The problem is that this generated "release-like" buildType crashes with tests and it does not really need to have them or to be tested since the relevant tests are already done in the other build types. The current code for skipping build types for unit tests is located in file AndroidUnitTestPlugin.groovy around line 114:

if (variant.buildType.name.equals(BuilderConstants.RELEASE)) {
    log("Skipping release build type.")
    return;
}

One easy alternative could be to change the above code to refer to buildTypes that inherit from 'release' instead of being exactly 'release'. If somebody know a url that describes the properties and methods of build types i could even try to made a pull request for this alternative myself, but i have not been able to find such documentation yet.

A more powerful alternative could be to add the ability to specify if a given build type is to be considered for unit tests or not in build.gradle file (which could default to true if it is not specified).

Regards, Juan Ramon Gonzalez

JurgenCruz commented 10 years ago

I'll consider a solution for this.

Meanwhile, there is something you can do. The test task will run all the test tasks. So for example, if you have debug, release and internalRelease, the following tasks will be generated: testDebug and testInternalRelease. You can run directly testDebug and it will not run the internalRelease tests. Assuming you have flavors: Free and Paid, then the generated tasks would be: testFreeDebug, testPaidDebug, testFreeInternalRelease and testPaidInternalRelease. You can make a new task that runs only testFreeDebug and testPaidDebug like this:

task onlyDebug(dependsOn: ['testFreeDebug', 'testPaidDebug']) {
  doLast {
    println 'Finished debug tests.'
  }
}
jrgonzalezg commented 10 years ago

Thanks, very helpful advice!

JurgenCruz commented 10 years ago

:smiley:

JurgenCruz commented 10 years ago

I haven't had time to make a better fix for this, but since there is a workaround I'll close it. A PR is welcome though.