How to run particular (out of build cycle) Junit5 tests using Gradle

前端 未结 2 470
一向
一向 2021-01-28 21:26

I am moving tests to junit5 that are going to be run with Gradle. In a project I work with there are unit tests and some specific tests that must be run on demand (from particul

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 21:59

    Define a new configuration, depend on junit-platform-console-standalone artifact and configure the console launcher to your needs. Like:

    configurations {
        standalone
    }
    
    dependencies {
        standalone 'org.junit.platform:junit-platform-console-standalone:1.0.0-SNAPSHOT'
    }
    
    task downloadJUnitPlatformStandalone(type: Copy) {
        from configurations.standalone
        into "$buildDir/junit-platform-standalone"
        eachFile { println " (standalone) -> " + it.file.name }
    }
    
    task runJUnitPlatformStandalone(type: JavaExec, dependsOn: downloadJUnitPlatformStandalone) {
        jvmArgs '-ea'
        jvmArgs '-Djava.util.logging.config.file=src/test/logging.properties'
        classpath = fileTree(dir: "$buildDir/junit-platform-standalone", include: '*.jar') + project.sourceSets.test.runtimeClasspath
        main 'org.junit.platform.console.ConsoleLauncher'
        args += '--scan-class-path'
        args += '--disable-ansi-colors'
        args += '--details=tree'
        args += "--reports-dir=$project.testReportDir"
    }
    
    test.dependsOn runJUnitPlatformStandalone
    

    Source junit-platform-standalone.gradle or alternate (Jupiter-only) dependencies jupiter.gradle.

    Without own configuration and download: https://discuss.gradle.org/t/junit-5-jupiter-platform-snapshot-console-launcher-task/19773/2

提交回复
热议问题