类别的Android单元测试

标签 android unit-testing android-gradle-plugin

我希望能够为我的 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/

相关文章:

android - 为什么我的项目会在我的手机上运行而不是我的 AVD?

unit-testing - 单元测试: How can you set attributes in a class

python - 在单元测试的上下文中,python 关键字 "is"与函数 id() 相同吗?

c# - AppDomain.CurrentDomain.RelativeSearchPath 在单元测试中为空

android-studio - Android Studio 不会停止要求更新 Gradle

android - Kotlin 错误 : Could not find org. jetbrains.kotlin :kotlin-stdlib-jre7:1. 0.7

c# - 低功耗蓝牙扫描仪 Xamarin Android

android - 为什么HtmlCompat.fromHtml()没有设置文本样式?

java - "fill_parent"和 "wrap_content"之间的区别

android - 我的应用程序只是构建并且从未在实际设备上安装 apk(Android Studio)