本文翻译自:How to run only one test class on gradle
I am new to Gradle. 我是Gradle的新手。 I use Gradle 1.10 and Ubuntu 13. 我使用Gradle 1.10和Ubuntu 13。
I want to know if there's any command to execute only one test class, similar to 'testonly' in SBT. 我想知道是否有任何命令只能执行一个测试类,类似于SBT中的“ testonly”。
#1楼
参考:https://stackoom.com/question/1WQiT/如何在gradle上仅运行一个测试类
#2楼
You can do gradle -Dtest.single=ClassUnderTestTest test
if you want to test single class or use regexp like gradle -Dtest.single=ClassName*Test test
you can find more examples of filtering classes for tests under this link section 23.12. 如果要测试单个类或使用诸如gradle -Dtest.single=ClassName*Test test
类的正则表达式,可以执行gradle -Dtest.single=ClassUnderTestTest test
,您可以在此链接第23.12节中找到更多过滤测试示例的示例。 Test. 测试。
#3楼
To run a single test class Airborn's answer is good. 要运行一个单独的测试班,Airborn的答案很好。
With using some command line options, which found here , you can simply do something like this. 使用此处提供的一些命令行选项,您可以简单地执行以下操作。
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
From version 1.10 of gradle it supports selecting tests, using a test filter . 从gradle的1.10版本开始,它支持使用测试过滤器选择测试。 For example, 例如,
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"
}
}
For multi-flavor environments (a common use-case for Android), check this answer , as the --tests
argument will be unsupported and you'll get an error. 对于多种口味的环境(Android的常见用例), 请检查此答案 ,因为--tests
参数将不受支持,并且会出现错误。
#4楼
In case you have a multi-module project : 如果您有一个多模块项目:
let us say your module structure is 假设您的模块结构是
root-module
-> a-module
-> b-module
and the test(testToRun) you are looking to run is in b-module, with full path : com.xyz.b.module.TestClass.testToRun 而您要运行的test(testToRun)在b模块中,具有完整路径:com.xyz.b.module.TestClass.testToRun
As here you are interested to run the test in b-module, so you should see the tasks available for b-module. 在这里,您有兴趣在b模块中运行测试,因此您应该看到可用于b模块的任务。
./gradlew :b-module:tasks
The above command will list all tasks in b-module with description. 上面的命令将列出带有描述的b模块中的所有任务。 And in ideal case, you will have a task named test to run the unit tests in that module. 在理想情况下,您将有一个名为test的任务,以在该模块中运行单元测试。
./gradlew :b-module:test
Now, you have reached the point for running all the tests in b-module, finally you can pass a parameter to the above task to run tests which matches the certain path pattern 现在,您已经可以在b模块中运行所有测试,最后可以将参数传递给上述任务以运行与特定路径模式匹配的测试
./gradlew :b-module:test --tests "com.xyz.b.module.TestClass.testToRun"
Now, instead of this if you run 现在,如果您运行,则代替此
./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"
It will run the test task for both module a and b, which might result in failure as there is nothing matching the above pattern in a-module. 它将同时为模块a和b运行测试任务,这可能会导致失败,因为在a模块中没有与上述模式匹配的内容。
#5楼
经过一番摸索,以下对我有用:
gradle test --tests "abcMyTestFile.mySingleTest"
#6楼
Please note that --tests
option may not work if you have different build types/flavors
(fails with Unknown command-line option '--tests'
). 请注意,如果您使用不同的构建types/flavors
,则--tests
选项可能不起作用(使用Unknown command-line option '--tests'
失败)。 In this case, it's necessary to specify the particular test task (eg testProdReleaseUnitTest
instead of just test
) 在这种情况下,必须指定特定的测试任务(例如, testProdReleaseUnitTest
而不是仅test
)