我希望能够为我的 Android 项目执行两个(或多个)测试任务,不同之处在于要包含/排除的一组不同的 Junit 类别。
使用 gradle java 插件,我可以做类似的事情
task testFast(type: Test) {
useJUnit {
includeCategories 'foo.Fast'
excludeCategories 'foo.Slow'
}
}
task testSlow(type: Test) {
useJUnit {
includeCategories 'foo.Slow'
excludeCategories 'foo.Fast'
}
}
但是,如果使用 android 插件,我必须将 testOptions 添加到 android 闭包中以包含/排除,
android {
...
testOptions {
unitTests.all {
useJUnit {
excludeCategories foo.Slow'
}
}
}
...
}
但这当然适用于所有构建变体的所有测试任务。
有没有办法创建使用相同构建变体但对不同类别执行测试的任务?
最佳答案
我想到的最好的方法是从命令行使用 gradle 属性:
testOptions {
unitTests.all {
useJUnit {
if (project.hasProperty('testCategory') && testCategory == "Slow") {
includeCategories 'foo.Slow'
} else {
excludeCategories 'foo.Slow'
}
}
}
}
和使用
gradlew -PtestCategory=Slow test
关于类别的Android单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459685/