Search code examples
unit-testinggradlejunitintegration-testing

Gradle : Multiple configurations for Test tasks


I have got two types of tests in my app as follows:

  • Unit tests (large number of tests and quick to execute)
  • Integration tests (small number of tests but each suite takes considerable time)

My project uses gradle and I want both sets of tests to execute concurrently. As per gradle's documentation, I can use maxParallelForks config to parallelize the execution. However, as gradle distributes tasks to workers statistically (see here) there is a chance that all my integration tests get allocated to the same worker.

So, what I really want is to have two sets of test blocks in my gradle file, e.g.:

test {
    include 'org/unit/**'
    exclude 'org/integration/**'
    maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
}

test {
    include 'org/integration/**'
    exclude 'org/unit/**'
    maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
}

Does gradle support two different test profiles like the above? If yes, can I execute those two in parallel?


Solution

  • I am assuming you have them all under the same source set: src/main/java/test.

    I'd suggest creating a separate source set and task specifically for integration tests. See Configuring integration tests.

    And since you want to parallelize the execution, then you'll need to create a custom task that submits both your unit and integration test tasks to the Worker API: https://guides.gradle.org/using-the-worker-api/