【问题标题】:Android Unit Testing of Categories类别的Android单元测试
【发布时间】:2016-02-17 14:38:12
【问题描述】:

我希望能够为我的 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插件,我必须在android闭包中添加testOptions来包含/排除,

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

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

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

【问题讨论】:

    标签: android unit-testing android-gradle-plugin


    【解决方案1】:

    我想出的最好办法是从命令行使用 gradle 属性:

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

    并使用

    gradlew -PtestCategory=Slow test
    

    【讨论】:

      【解决方案2】:

      我需要一个不需要额外 gradle 属性的解决方案,所以我想出了这个。不过,您确实需要确保在一次运行中最多只执行一个测试任务。

      我希望 android 插件能在未来的版本中使这更容易。

      task testFast(dependsOn: 'testDebugUnitTest')
      task testSlow(dependsOn: 'testDebugUnitTest')
      
      gradle.taskGraph.whenReady { taskGraph ->
          def testTask = tasks.getByName("testDebugUnitTest")
      
          if (taskGraph.hasTask(tasks.getByName("testFast"))) {
              testTask.useJUnit {
                  includeCategories 'foo.Fast'
                  excludeCategories 'foo.Slow'
              }
          }
      
          if (taskGraph.hasTask(tasks.getByName("testSlow")))
              testTask.useJUnit {
                  includeCategories 'foo.Slow'
                  excludeCategories 'foo.Fast'
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多