Search code examples
gradlejunitjunit5

How to disable all tests per default and only execute those that comply with a certain condition


We run our junit5 tests on certain environments (dev, preview, prod). On dev and preview, I'd like to run all available tests without further ado. On prod, I'd like to only execute a rather small subset of these tests.

Afaik, one could solve this by annotating all tests that should not run on prod with something like:

@EnabledIfSystemProperty(named = "profile", matches = "dev|preview")
annotation class DevAndPreviewOnly

I wonder if there is a possibility to configure this the other way round, by explicitly specifying which tests should be executed on prod and skipping the other tests per default?


Solution

  • You could (only) tag the tests that should run on prod …

    @Tag("prod")
    public class MyTests {
        // …
    }
    

    … and then use a Gradle configuration like this to decide which tests to run per build:

    test {
        useJUnitPlatform {
            if (project.hasProperty('prod')) {
                includeTags 'prod'
            }
            // otherwise run all tests
        }
    }