Android单元类别测试

前端之家收集整理的这篇文章主要介绍了Android单元类别测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望能够为我的 Android项目提供两个(或多个)测试任务,其中差异是要包含/排除的一组不同的Junit类别.

使用gradle java插件,我可以做类似的事情

  1. task testFast(type: Test) {
  2. useJUnit {
  3. includeCategories 'foo.Fast'
  4. excludeCategories 'foo.Slow'
  5. }
  6. }
  7.  
  8. task testSlow(type: Test) {
  9. useJUnit {
  10. includeCategories 'foo.Slow'
  11. excludeCategories 'foo.Fast'
  12. }
  13. }

但是,如果使用android插件,我必须将testOptions添加到android闭包中以包含/排除,

  1. android {
  2. ...
  3. testOptions {
  4. unitTests.all {
  5. useJUnit {
  6. excludeCategories foo.Slow'
  7. }
  8. }
  9. }
  10. ...
  11. }

但当然这适用于所有构建变体的所有测试任务.

有没有办法创建使用相同构建变体的任务,但是对不同类别执行测试?

解决方法

我想出的最好的方法是从命令行使用gradle属性
  1. testOptions {
  2. unitTests.all {
  3. useJUnit {
  4. if (project.hasProperty('testCategory') && testCategory == "Slow") {
  5. includeCategories 'foo.Slow'
  6. } else {
  7. excludeCategories 'foo.Slow'
  8. }
  9. }
  10. }
  11. }

并使用

  1. gradlew -PtestCategory=Slow test

猜你在找的Android相关文章