Search code examples
gradlejvmbuild.gradlejunit5spring-boot-test

Is there a way to define group of test classes to run under same Gradle JVM fork?


I have a set of Spring Boot integration tests I am trying to optimise. For that I was trying to leverage the Application Context caching, however in the docs it says, as it is being stored in a static variable, that when using the Gradle forked JVMs test execution this won't be used as effectively.

So I was wondering if there is a way to somehow define what group of test classes Gradle should run under the same fork; for example the ones that could share the same @WebMvcTest sliced context.

E.g. Having these test classes:

- ATest
- BTest
- CTest

I’d like to specify something like:

- Fork 1:
  - ATest
  - BTest

- Fork 2:
  - CTest

I only found that the JUnit Platform provides Tags annotations to logically group tests and then include/exclude them in the Gradle Test task config; but I didn't found if it could be used with the forking somehow.

Is there a way to achieve this with either Gradle or Junit config? (or even maybe with multiple Gradle Test task that I could run in parallel)


Solution

  • If you don't configure it explicitly, Gradle will only do one fork. If you want to fork into multiple test workers, then afair you cannot control which test goes where.

    But yes, you could configure multiple tasks that all just do one fork and control for example using tags which task runs which tests.

    Another possibility would be to use a JUnit Platform engine that supports parallel test execution. Spock and Jupiter for example both support that, so your test can leverage your multi-processors and run in parallel while staying within one JVM.