Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Unit Testing of Categories

I'd like to be able to have two (or multiple) test tasks for my Android project, where the difference is a different set of Junit Categories to include/exclude.

Using the gradle java plugin, I can do something like

task testFast(type: Test) {
    useJUnit {
        includeCategories 'foo.Fast'
        excludeCategories 'foo.Slow'
    }
}

task testSlow(type: Test) {
    useJUnit {
        includeCategories 'foo.Slow'
        excludeCategories 'foo.Fast'
    }
}

However, if using the android plugin, I have to add testOptions to the android closure to include/exclude,

android {
...
    testOptions {
        unitTests.all {
            useJUnit {
                excludeCategories foo.Slow'
            }
        }
    }
...
}

but of course that applies to all test tasks for all build variants.

Is there a way to create tasks that use the same build variant, but execute tests on different categories?

like image 988
JatraTim Avatar asked Feb 17 '16 14:02

JatraTim


1 Answers

Best I've come up with is to use a gradle property from the command line:

testOptions {
    unitTests.all {
        useJUnit {
            if (project.hasProperty('testCategory') && testCategory == "Slow") {
                includeCategories 'foo.Slow'
            } else {
                excludeCategories 'foo.Slow'
            }
        }
    }
}

and use

gradlew -PtestCategory=Slow test
like image 172
JatraTim Avatar answered Oct 07 '22 19:10

JatraTim