821

I want to execute gradle build without executing the unit tests. I tried:

$ gradle -Dskip.tests build

That doesn't seem to do anything. Is there some other command I could use?

zb226
  • 9,586
  • 6
  • 49
  • 79
Dave
  • 13,518
  • 7
  • 42
  • 51
  • 1
    Maybe this helps you [Gradle Buil without tests](http://stackoverflow.com/questions/14779768/how-can-i-not-include-a-build-task-when-i-include-a-project-in-my-settings-gradl/24138470#24138470) – rogue lad Jun 10 '14 at 10:23
  • 13
    `-DskipTests` is for Maven – Gayan Weerakutti Oct 31 '19 at 09:02
  • I understand it can be helpful sometimes, but Gradle has easy to enable test concurrency. Maybe shaving 1-2 seconds is not the best thing, so making sure we spend less time running them could allow us to always run tests and not pay for the cost or pay little. – Mashimom Feb 12 '21 at 08:11

11 Answers11

1569

You should use the -x command line argument which excludes any task.

Try:

gradle build -x test 

Update:

The link in Peter's comment changed. Here is the diagram from the Gradle user's guide

David Avendasora
  • 4,538
  • 1
  • 16
  • 15
c_maker
  • 19,076
  • 1
  • 24
  • 29
  • 51
    This is the correct answer. 'gradle assemble' will leave out many other tasks too. See this [diagram](http://gradle.org/latest/docs/userguide/userguide_single.html#N119C7) to get an idea. For typical real-life builds, 'gradle assemble' will leave out even more tasks. – Peter Niederwieser Feb 03 '11 at 16:48
  • 13
    Link from @PeterNiederwieser no longer valid. Probably he was referring to this diagram: http://gradle.org/docs/current/userguide/img/javaPluginTasks.png – Dave L. Aug 12 '12 at 18:23
  • 12
    Works though you may need to add e.g. `-x integTest` and so on, so not as convenient as Maven’s blanket `-DskipTests`. – Jesse Glick Aug 16 '13 at 16:37
  • 1
    @c_maker, How to perform this from eclipse? – rinuthomaz Jun 21 '16 at 14:55
  • 1
    By the way, you can exclude not all, but the certain module's (i.e. for `module1`) tests using next syntax: `gradle build -x :module1:test` – Anton Ermakov May 31 '17 at 11:37
  • As @PeterNiederwieser said, just don't call `gradle build`, but call the task you are interested in instead. If all you care about is the artifacts of the project, `gradle assemble` would be appropriate. Using `-x` is almost never necessary. – Stefan Oehme Jul 13 '17 at 11:05
  • In my case I'm using the `artifactoryPublish` Gradle plugin and testing in a separate step. Tests kept failing only when the plugin executed and I don't want the publishing plugin to test code for me so this answer worked a treat. Thanks! – Victor Ude Aug 06 '18 at 19:01
  • Based on your update, and on a multi-project grails app I'm working on, `-x check` worked where `-x test` did not. – Greg Aug 11 '18 at 03:00
  • `gradlew build -x integrationTest` in my case with an older gradle (`2.9`) and groovy (`2.4.4`). – Binita Bharati Aug 28 '20 at 12:41
137

Try:

gradle assemble

To list all available tasks for your project, try:

gradle tasks

UPDATE:

This may not seem the most correct answer at first, but read carefully gradle tasks output or docs.

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Emil Sit
  • 22,894
  • 7
  • 53
  • 75
  • 11
    As an additional hint in case you are debugging your unit tests: `gradle assemble` will not compile the main files. If you work in getting your unit test fixed then you need `gradle assemble testClasses` — I think that the build task naming is quite confusing. – Martin Feb 25 '15 at 07:47
  • 1
    I think that this answer should be the chosen one. – marverix Nov 09 '22 at 06:51
53

You can add the following lines to build.gradle, **/* excludes all the tests.

test {
    exclude '**/*'
}
Guisong He
  • 1,886
  • 1
  • 15
  • 27
41

The accepted answer is the correct one.

OTOH, the way I previously solved this was to add the following to all projects:

test.onlyIf { ! Boolean.getBoolean('skip.tests') }

Run the build with -Dskip.tests=true and all test tasks will be skipped.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
David Resnick
  • 4,891
  • 5
  • 38
  • 42
  • 3
    For me this is still the only working option as the -x test seems to only work on the starting project but not for dependencies in a multi project build (tested on gradle 2.1). Put the test.onlyif ... in a configure(subprojects.findAll {javaProjects.contains(it.name)}) {} block in your root build.gradle and it will work for all projects. – timm Oct 11 '14 at 08:07
25

Using -x test skip test execution but this also exclude test code compilation.

gradle build -x test 

In our case, we have a CI/CD process where one goal is compilation and next goal is testing (Build -> Test).

So, for our first Build goal we wanted to ensure that the whole project compiles well. For this we have used:

./gradlew build testClasses -x test

On the next goal we simply execute tests:

./gradlew test
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • Does `./gradlew build testClasses -x test` just build testClasses? or does it build regular classes too? Do you need to run both `./gradlew build testClasses -x test` and `./gradlew build -x test` – red888 May 19 '22 at 19:33
  • @red888 The `build` builds regular ones while `testClasses` build test ones. You can check the different tasks here https://docs.gradle.org/current/userguide/java_plugin.html – Federico Piazza May 20 '22 at 00:17
21

Every action in gradle is a task, and so is test. And to exclude a task from gradle run, you can use the option --exclude-task or it's shorthand -x followed by the task name which needs to be excluded. Example:

gradle build -x test

The -x option should be repeated for all the tasks that needs to be excluded.

If you have different tasks for different type of tests in your build.gradle file, then you need to skip all those tasks that executes test. Say you have a task test which executes unit-tests and a task testFunctional which executes functional-tests. In this case, you can exclude all tests like below:

gradle build -x test -x testFunctional
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
10

You can exclude tasks

 gradle build --exclude-task test 

https://docs.gradle.org/current/userguide/command_line_interface.html#sec:command_line_executing_tasks

Pehmolelu
  • 3,534
  • 2
  • 26
  • 31
7

the different way to disable test tasks in the project is:

tasks.withType(Test) {enabled = false}

this behavior needed sometimes if you want to disable tests in one of a project(or the group of projects).

This way working for the all kind of test task, not just a java 'tests'. Also, this way is safe. Here's what I mean let's say: you have a set of projects in different languages: if we try to add this kind of record in main build.gradle:

 subprojects{
 .......
 tests.enabled=false
 .......
}

we will fail in a project when if we have no task called tests

Sergei Iakovlev
  • 356
  • 4
  • 10
5

Reference

To exclude any task from gradle use -x command-line option. See the below example

task compile << {
    println 'task compile'
}

task compileTest(dependsOn: compile) << {
    println 'compile test'
}

task runningTest(dependsOn: compileTest) << {
    println 'running test'
}
task dist(dependsOn:[runningTest, compileTest, compile]) << {
    println 'running distribution job'
}

Output of: gradle -q dist -x runningTest

task compile
compile test
running distribution job

Hope this would give you the basic

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
2

In The Java Plugin:

$ gradle tasks

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
testClasses - Assembles test classes.

Verification tasks
------------------
test - Runs the unit tests.

Gradle build without test you have two options:

$ gradle assemble
$ gradle build -x test

but if you want compile test:

$ gradle assemble testClasses
$ gradle testClasses
-1

Please try this:

gradlew -DskipTests=true build

d4vsanchez
  • 1,904
  • 1
  • 14
  • 24
  • 6
    @KirankumarDafda - no. You can post an answer whenever you want, even if an existing answer had been accepted. The new answer might be better than the accepted answer, or solve it a different way, or otherwise help others. [From Review](https://stackoverflow.com/review/low-quality-posts/23454025). – Wai Ha Lee Jul 06 '19 at 09:23
  • @WaiHaLee I cannot able to edit my comment, but as I got suggestions for new users, I wanted to explain that try to add answers with more clarifications so other user can check if why this answer or code is helpful to them. – Kirankumar Dafda Jul 06 '19 at 09:40