How to sort test classes into a specific batches?

I’ve got 100s of Junit test classes, I want to group them into batches in order to run them in sequence or in parallel.

I want to be able to execute **/* (ideally *Test.groovy too) and then define the order in which the matching tests are run. The configuration could be by a particular annotation or some other matching criteria to determine the batch.

I would like to do this because ant, by default, selects test classes for execution in alphabetical order of their full class names eg. [,]. However a sparse selection of test classes in our codebase use custom spring application contexts and it might speed up test execution if I could order test classes by their Spring application context i.e. by the value in the @ContextConfiguration annotation.

Any suggestions of how I could do this?

Not sure if grouping tests into batches will speed up your tests. The only potential advantage I can see is having fewer application contexts open at the same time (since Spring’s TestContext framework caches them). Also, grouping tests into batches won’t really help for running them in parallel, as tasks in the same project are currently always executed sequentially (even with the experimental ‘–parallel’ switch). Have you tried to let Gradle run tests in parallel by setting ‘test.maxParallelForks’?

Anyway, to group tests into batches you’ll either have to add and configure additional tasks of type ‘Test’, or reconfigure the one added by the Java plugin. (In the latter case, you’ll only be able to run one batch per execution of Gradle.) In particular, you’ll have to work with ‘test.include’ and ‘test.exclude’. For details, see the DSL reference for the ‘Test’ task type, and the ‘withIntegrationTests’ sample in the full Gradle distribution.