How to run two test suites in the JUnit Platform in Gradle

I am using the JUnit 5 platform through Gradle.

There is a configuration condition in the current build file

junitPlatform {
    platformVersion '1.0.0-M5'
    logManager 'java.util.logging.LogManager'
    enableStandardTestTask true

    filters {
        tags {
            exclude 'integration-test'
        }
        packages {
            include 'com.scherule.calendaring'
        }
    }
}

It works great. But I also need to perform integration tests, which require the application to be built, docked and run in the background. Therefore, I should have a second configuration, like this one, that will only be launched then ... how to do this? Usually I would like to extend the test task by creating an IntegrationTest task, but it is not suitable for the JUnit platform, where there are no simple tasks that perform tests ...

I know I can do it like this.

task integrationTests(dependsOn: "startMyAppContainer") {
    doLast {
        def request = LauncherDiscoveryRequestBuilder.request()
                .selectors(selectPackage("com.scherule.calendaring"))
                .filters(includeClassNamePatterns(".*IntegrationTest"))
                .build()

        def launcher = LauncherFactory.create()

        def listener = new SummaryGeneratingListener()
        launcher.registerTestExecutionListeners(listener)
        launcher.execute(request)
    }

    finalizedBy(stopMyAppContainer)
}

but is there an easier way? More consistent.

+4
source share
1

Gradle JUnit5 ( ). . , : , , maven test vs. verify.

() .

Gradle main test source , . Integration, . , , Set, , ( ". IntegrationTest" Set IntegrationTest sourceSet). , .

sourceSets {
  integrationTest {
    java {
      compileClasspath += main.output + test.output
      runtimeClasspath += main.output + test.output
      srcDir file('src/integrationTest/java')
    }
    resources.srcDir file('src/integrationTest/resources')
  }
}

java, integrationTestCompile integrationTestRuntime dependencies:

dependencies {
    // .. other stuff snipped out ..
    testCompile "org.assertj:assertj-core:${assertjVersion}"

    integrationTestCompile("org.springframework.boot:spring-boot-starter-test") {
        exclude module: 'junit:junit'
    }
}

!

, . , ; , .

def integrationTest = task('integrationTest',
                           type: JavaExec,
                           group: 'Verification') {
    description = 'Runs integration tests.'
    dependsOn testClasses
    shouldRunAfter test
    classpath = sourceSets.integrationTest.runtimeClasspath

    main = 'org.junit.platform.console.ConsoleLauncher'
    args = ['--scan-class-path',
            sourceSets.integrationTest.output.classesDir.absolutePath,
            '--reports-dir', "${buildDir}/test-results/junit-integrationTest"]
}

dependOn shouldRunAfter, , . , , ./gradlew check, :

check {
  dependsOn integrationTest
}

./gradlew test ./mvnw test ./gradlew check ./mvnw verify.

+6

Source: https://habr.com/ru/post/1682433/


All Articles