我是 Gradle 的新手。我使用 Gradle 1.10 和 Ubuntu 13。
我想知道是否有任何命令只执行一个单元测试类,类似于 SBT 中的 testOnly
。
运行单个测试类 Airborn 的答案很好。
通过使用找到 here 的一些命令行选项,您可以简单地执行类似的操作。
gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests *SomeTest.someSpecificFeature
gradle test --tests *SomeSpecificTest
gradle test --tests all.in.specific.package*
gradle test --tests *IntegTest
gradle test --tests *IntegTest*ui*
gradle test --tests *IntegTest.singleMethod
gradle someTestTask --tests *UiTest someOtherTestTask --tests *WebTest*ui
从 1.10 版的 gradle 开始,它支持使用 test filter 选择测试。例如,
apply plugin: 'java'
test {
filter {
//specific test method
includeTestsMatching "org.gradle.SomeTest.someSpecificFeature"
//specific test method, use wildcard for packages
includeTestsMatching "*SomeTest.someSpecificFeature"
//specific test class
includeTestsMatching "org.gradle.SomeTest"
//specific test class, wildcard for packages
includeTestsMatching "*.SomeTest"
//all classes in package, recursively
includeTestsMatching "com.gradle.tooling.*"
//all integration tests, by naming convention
includeTestsMatching "*IntegTest"
//only ui tests from integration tests, by some naming convention
includeTestsMatching "*IntegTest*ui"
}
}
对于多风格环境(Android 的常见用例),check this answer,因为 --tests
参数将不受支持,您会收到错误消息。
在 Gradle 5 之前的版本中,test.single
系统属性可用于指定单个测试。
如果您想测试单个类或使用像 gradle -Dtest.single=ClassName*Test test
这样的正则表达式,您可以执行 gradle -Dtest.single=ClassUnderTestTest test
,您可以找到更多用于测试的过滤类示例 under this link。
Gradle 5 删除了此选项,因为它已被使用 --tests
命令行选项的 test filtering 取代。
如果您有一个多模块项目:
让我们说你的模块结构是
root-module
-> a-module
-> b-module
并且您要运行的测试(testToRun)位于 b 模块中,具有完整路径:com.xyz.b.module.TestClass.testToRun
在这里,您有兴趣在 b-module 中运行测试,因此您应该看到可用于 b-module 的任务。
./gradlew :b-module:tasks
上面的命令将列出 b-module 中的所有任务和描述。在理想情况下,您将有一个名为 test 的任务来运行该模块中的单元测试。
./gradlew :b-module:test
现在,您已经到了在 b-module 中运行所有测试的地步,最后您可以将参数传递给上述任务以运行与特定路径模式匹配的测试
./gradlew :b-module:test --tests "com.xyz.b.module.TestClass.testToRun"
现在,如果你运行,而不是这个
./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"
它将为模块 a 和 b 运行测试任务,这可能会导致失败,因为在 a-module 中没有与上述模式匹配的内容。
./gradlew :b-module:test --tests testToRun
请注意,如果您有不同的构建 types/flavors
,--tests
选项可能不起作用(Unknown command-line option '--tests'
失败)。在这种情况下,有必要指定特定的测试任务(例如 testProdReleaseUnitTest
而不仅仅是 test
)
./gradlew test --tests *testMyCollectionTake*
运行 3 个与通配符匹配的测试,但我不知道应该如何将 test
替换为通配符测试名称
test
任务;例如,不能使用 check
任务。
./gradlew testDebug --tests "*testMyCollectionTake*"
经过深思熟虑,以下内容对我有用:
gradle test --tests "a.b.c.MyTestFile.mySingleTest"
对于多模块项目,必须使用模块名称和 buildType:
./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.*
要运行某些测试方法,使用相同的命令,但使用测试的名称:
./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.test
以下是使用 gradlew
命令行选项运行单个测试类的命令:
gradlew.bat Connected**your bundleVariant**AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass
以下示例使用变体 Variant_1
运行类 com.example.TestClass
:
gradlew.bat ConnectedVariant_1AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass
运行一个名为 MyTest 的测试:
./gradlew app:testDebug --tests=com.example.MyTest
您应该尝试在末尾添加星号 (*)。
gradle 测试 --tests "com.abc*"
就我而言,我的 eclipse java 编译器警告设置得太高,并且 eclipse 没有将我的类识别为有效执行。更新我的编译器设置解决了这个问题。您可以在此处阅读更多信息:annotation-nonnull-cannot-be-resolved
这对我有用
发布案例:gradle testReleaseUnitTest --tests testClass
调试案例:gradle testDebugUnitTest --tests AsyncExecutorTest
您可以通过以下方式查看项目:gradle -q projects
并移至要测试的类所在的项目
不定期副业成功案例分享
--tests
在类中调用单个测试方法,我们也会看到所有类的迭代,甚至所有未运行的类,这太慢了。不知道这种缓慢是由于我们的环境还是会影响其他人。如果发现更多可以更新。gradle test --tests org.gradle.SomeTest$NestedClassTest
singleMethod
对我不起作用(不再?)。