How to run only one test class on gradle

2020-01-23 12:55发布

I am new to Gradle. I use Gradle 1.10 and Ubuntu 13.

I want to know if there's any command to execute only one test class, similar to 'testonly' in SBT.

9条回答
姐就是有狂的资本
2楼-- · 2020-01-23 13:35

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

As here you are interested to run the test in b-module, so you should see the tasks available for b-module.

./gradlew :b-module:tasks

The above command will list all tasks in b-module with description. And in ideal case, you will have a task named test to run the unit tests in that module.

./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

./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.

查看更多
在下西门庆
3楼-- · 2020-01-23 13:38

In my case, my eclipse java compiler warnings were set too high, and eclipse was not recognizing my class as valid for execution. Updating my compiler settings fixed the problem. You can read more about it here: annotation-nonnull-cannot-be-resolved

查看更多
迷人小祖宗
4楼-- · 2020-01-23 13:39

Please note that --tests option may not work if you have different build types/flavors (fails with Unknown command-line option '--tests'). In this case, it's necessary to specify the particular test task (e.g. testProdReleaseUnitTest instead of just test)

查看更多
Bombasti
5楼-- · 2020-01-23 13:46

After much figuring out, the following worked for me:

gradle test --tests "a.b.c.MyTestFile.mySingleTest"

查看更多
欢心
6楼-- · 2020-01-23 13:48

Run a single test called MyTest:

./gradlew app:testDebug --tests=com.example.MyTest
查看更多
在下西门庆
7楼-- · 2020-01-23 13:52

You should try to add asteriks (*) to the end.

gradle test --tests "com.a.b.c.*"

查看更多
登录 后发表回答