3

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?

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102

2 Answers2

3

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/

Cisco
  • 20,972
  • 5
  • 38
  • 60
0

Starting from Gradle 7.4 the The built-in JVM Test Suite Plugin is the way to go:

testing {
    suites { 
        test { 
            useJUnitJupiter() 
        }

        integrationTest(JvmTestSuite) { 
            dependencies {
                implementation project
                // other integration-test-specific dependencies
            }

            targets { 
                all {
                    testTask.configure {
                        shouldRunAfter(test)
                        maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
                    }
                }
            }
        }
    }
}
jannis
  • 4,843
  • 1
  • 23
  • 53