Gradle build command running integrationTest task

Context of my application structure:

  1. Nothing on root, just settings.gradle and build.gradle with minimal dependency.
  2. There are many module/subproject, one of them named xyzintegrationtest
  3. the integrationtest module xyzintegrationtest has src/main, src/test and src/integrationtest
  4. The src/main and src/test are empty. src/intgerationtest has integration tests using junit.
  5. The integrationtest build.gradle file has following task:

task integrationTest(type: Test) {

}

Now when I do gradle build (or gradlew build) on root project I expect all projects to compile, and all unit tests to run that are under src/test of each subproject. But it is also calling this integrationTest task, and making integration tests run as well. And more surprisingly it happens sporadically, not consistently. I even tried gradle build -x integrationTest, but it still runs.

By default, integration tests will run when the “build” task is run. Specifically, the “check” task is depended on by “build”, and “check” causes all “Test” tasks to run.

If you want to prevent integration tests from running by default (which I would agree with), then you need something like the following:

// Makes integration tests not run by default.
check.dependsOn -= integTest

Where “integTest” is the name of your integration test task.

Thanks, let me try it.

Thank you again, that worked. Also someone suggested to have task like this:

task integrationTest(type: Test) << {…} so it does not run in configuration phase. But when I tried it, and then I wanted it to explicitly run then it did not run still.

Well, yes, you need to use either the “<<” notation or enclose all of the body in a “doLast” block.

By your last statement (I had a feeling you would mention this next), I assume you mean that after you saw it run once, you wanted to run it again, and it refused to do anything. That’s because by default all Gradle tasks will only run if it is found that one or more of its inputs or outputs are out of date (that’s not quite technically complete, but it’s a common phrasing of the requirement). In short, if a task runs, and then you try to run it again, it will assume you didn’t want it to run again, as it generally assumes tasks are idempotent, so there’s no point in running it again if it doesn’t seem as if it will come out any differently.

You might conclude that you wouldn’t want it to work this way for your integration test task, and I would agree. You want the task to run every time you ask it to run, no matter what. Fortunately, this is easy to do, although the solution is likely very non-obvious.

Just put something like this in your build script:

project.integTest {
  // This forces integration tests to always run if the task is run.
  outputs.upToDateWhen { false }
}