Don't run tests unless test task specified on command line

Note: The build I’m working on is a multi-project build. This may impact what solutions will work.

I have a project with “unit tests” that are in pretty bad shape. Running all of them for the entire project takes far too long on a developer laptop. We want them to run on Jenkins or if the user explicitly runs gradle test or gradle clean test or something like that.

How can I modify my build so that tests are run when explicitly requested and skipped when they aren’t requested? I don’t want every developer to have to specify -x test every time they run the tests, especially because this is a multi-project build and some of the projects have unit tests that I’d like to run every time (because they’re actually decent unit tests).

Things I’ve tried:

  1. check.dependsOn.remote(test) which works perfectly (but is deprecated and will be removed).
  2. test.enabled = false which doesn’t work (it doesn’t run the tests when they’re explicitly requested, i.e., it behaves as expected but not as desired).

A thought I’ve had: Is there some way to check the command-line arguments to see if test was explicitly requested as a task and disable it if it wasn’t explicitly requested?

A dumb solution would be to just make your build aware that it;'s running on jenkins and utilze solution nr 2 . Just add some project property from command line: ./gradlew -Pjenkins=true … and use that:
test.enable = project.hasPropety(‘jenkins’) && jenkins

The test will be disabled if nothing is supplied - on developer machine

Sometimes the developers want to run tests and I don’t want them to have to remember to add -Pjenkins=true.


you could try something like this
tasks.test {
enabled = gradle.startParameter.taskNames.contains(“test”)

That did the trick! Thank you!