I would like to run a Gradle build that compiles the tests (JUnit tests in the src/test/java
directory) but does not run them.
./gradlew build
compiles and runs the tests, while ./gradlew build -x test
does not compile the tests. I also tried ./gradlew build -x testClasses
, but that does not yield the desired result as it builds and runs the tests.
Is there a way to achieve this?
To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build.
By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.
In versions of Gradle prior to 5, the test. single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest.
Parallel execution Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.
TL;DR: gradle testClasses
compiles the test source code.
The tasks for compiling the test source code will not be needed when the test
-task is excluded. Gradle sees that and won't execute the unneeded tasks.
Here is the task dependency graph, when you exclude something all tasks that arent needed for other tasks will be dropped. source Gradle User Guide
So looking at the task dependency graph testClasses
is the task for compiling the test source code and processing the resources.
You could just add this task to your command,
gradle build testClasses -x test
Or even better use the assemble
-task (build the jar) with the testClasses
-task. With this, you do not have to exclude the tests.
gradle assemble testClasses
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With