Kotlin Gradle DSL to exclude acceptance test when running unit tests

Viewed 1517

I using the gradle kotlin DSL and I need to exclude my acceptance tests when running build or test. Currently I have

tasks.withType<Test> {
   exclude("*acceptance*")

}

Which doesn't work. The acceptance test is executed, it is not excluded.

I also need to configure my build.gradle.kts to run my acceptance tests. At the moment I have an acceptance directory which is under is under src/test/groovy and I have created a acceptanceTest task as per the documentation here. The acceptance test is not run with this configuration.

sourceSets {
    create("acceptance") {
        compileClasspath += sourceSets.main.get().output
        runtimeClasspath += sourceSets.main.get().output
    }
}

val acceptanceImplementation by configurations.getting {
    extendsFrom(configurations.implementation.get())
}

val acceptanceTest = task<Test>("acceptanceTest") {
    description = "Runs Acceptance tests."
    group = "verification"

    testClassesDirs = sourceSets["test"].output.classesDirs
    classpath = sourceSets["acceptance"].runtimeClasspath
    shouldRunAfter("test")
}

tasks.check { dependsOn(acceptanceTest) }
1 Answers

The exclude pattern uses Ant-style path matching, so, in order to exclude all tests in a directory named acceptance, you need to do:

exclude("**/acceptance/**")

However, doing so for all tasks of type Test as you've shown will never run the acceptance tests. You need to skip it only for unit tests, which is the task named test.

To run the acceptance tests, you'll use the same path pattern with include inside the acceptanceTest task.

Related