[英]How do we exclude some unit tests from running in an Android project
We are in我们在
And I can run a subset of tests with我可以运行测试的子集
./gradlew :app:testDebug --tests="*Fragment*" --tests="*Activity*"
But I want to find a way to do the inverse ie not run the tests that is matching these options, something like但我想找到一种方法来做相反的事情,即不运行与这些选项匹配的测试,比如
./gradlew :app:testDebug --notTests="*Fragment*" --notTests="*Activity*"
There is an excludeTests option https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestFilter.html but I can't use these in an android project, these are pure java options.有一个 excludeTests 选项https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestFilter.html但我不能在 android 项目中使用这些,这些是纯 java 选项。
Some context: On our CI we are seeing memory issues with unit tests and we are suspecting roboelectric, so we are trying separate pure unit tests vs roboelectric tests to try and isolate the issues or apply specific fixes for the jobs.一些背景:在我们的 CI 上,我们看到单元测试存在内存问题,我们怀疑是机器人电气,所以我们正在尝试单独的纯单元测试与机器人电气测试,以尝试隔离问题或为作业应用特定的修复程序。
I know it's not the nicest solution but I did manage to make it work by doing the following.我知道这不是最好的解决方案,但我确实通过执行以下操作设法使其工作。
Annotate your test class with注释您的测试类
@Category(my.package.WhateverClass) // I personally used Robolectric
Add the following to your root build.gradle
将以下内容添加到您的根build.gradle
if (project.gradle.startParameter?.taskRequests?.args[0]?.remove("--robolectric")) {
subprojects
.all {
it.tasks
.withType(Test)
.configureEach {
println "Configuring task $it.name"
useJUnit {
includeCategories 'my.package.WhateverClass'
}
}
}
} else if (project.gradle.startParameter?.taskRequests?.args[0]?.remove("--unit")) {
subprojects
.all {
it.tasks
.withType(Test)
.configureEach {
println "Configuring task $it.name"
useJUnit {
excludeCategories 'my.package.WhateverClass'
}
}
}
}
Run tests as ./gradlew project:testDebug --robolectric
or ./gradlew project:testDebug --unit
作为./gradlew project:testDebug --robolectric
或./gradlew project:testDebug --unit
运行测试
If you don't add anything it will run all tests by default如果您不添加任何内容,它将默认运行所有测试
If you have more time, you could probably create a task, that extends from test and adds the same configurations.如果您有更多时间,您可能可以创建一个任务,从测试扩展并添加相同的配置。
Note: The .remove('--robolectric')
is there because Gradle will think its a task since it's not defined as a flag in any task注意: .remove('--robolectric')
存在是因为 Gradle 会认为它是一个任务,因为它没有在任何任务中定义为标志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.