Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: compile tests but do not run them

Tags:

gradle

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?

like image 371
Gabor Szarnyas Avatar asked Apr 26 '17 15:04

Gabor Szarnyas


People also ask

How do you exclude test cases in build Gradle?

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.

Does Gradle build also run tests?

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.

How do I run a single test file in Gradle?

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.

Does Gradle run tests in parallel by default?

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.


1 Answers

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. Java Task Graph 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
like image 175
jmattheis Avatar answered Oct 20 '22 11:10

jmattheis