首页 文章

如何使用Gradle运行特定的(在构建周期之外)Junit5测试

提问于
浏览
0

我正在将测试转移到将要使用Gradle运行的junit5 . 在我工作的项目中,有单元测试和一些必须按需运行的特定测试(我想从特定的Gradle任务) .

关于单元测试很清楚 . Gradle插件增加了对它的支持 . 但我无法找到一种方法来定义另一个测试任务以满足我的需求,我在Junit5插件源中搜索并发现没有任何特定的类用于此目的 . Gradle插件只是设置一个JavaExec任务然后运行它 .

因此,似乎没有可见的方法来定义我自己的内置类型的任务,就像这样

task myTask(type:Junit5TestRunner)这里我们设置一个任务

有什么想法可以做到吗?

2 回答

  • 2

    定义新配置,依赖于 junit-platform-console-standalone 工件并根据需要配置控制台启动器 . 喜欢:

    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
    

    junit-platform-standalone.gradle或备用(仅限Jupiter)依赖项jupiter.gradle .

    没有自己的配置和下载:https://discuss.gradle.org/t/junit-5-jupiter-platform-snapshot-console-launcher-task/19773/2

  • 0

    似乎我找到了更好的决定

    task myTask(type:Junit5TestRunner)这里我们设置一个任务

    Sormuras的答案给了我一个正确的方向解决方案是将最多的样板代码移动到单独的任务类中,然后从脚本中使用该任务,从而使其更具可重用性 .

    class

    /**
     *
     * Created by Vladimir Bogodkhov on 21/04/17.
     * @author Vladimir Bogodkhov
     */
    class SQJUnit5 extends JavaExec {
    
        enum Details {
    
            /**
             * No test plan execution details are printed.
             */
            none("none"),
    
            /**
             * Test plan execution details are rendered in a flat, line-by-line mode.
             */
                    flat("flat"),
    
            /**
             * Test plan execution details are rendered as a simple tree.
             */
                    tree("tree"),
    
            /**
             * Combines tree flat modes.
             */
                    verbose("verbose");
    
            Details(String id) {
                this.id = Objects.requireNonNull(id);
            }
    
            final String id
    
        }
    
    
        List<String> includeTags
        List<String> excludeTags
        List<String> includeTests = ['^.*Tests?$']
        List<String> excludeTests
        File reportsdir
        Details details = Details.none;
        List<String> scanclasspath
    
    
        SQJUnit5() {
            jvmArgs '-ea'
            main 'org.junit.platform.console.ConsoleLauncher'
            args += '--disable-ansi-colors'
            args += '--details=tree'
            args += '--details-theme=unicode'
        }
    
        @Override
        void exec() {
            prepare()
            super.exec()
        }
    
        private void prepare() {
            if (includeTags) includeTags.each { args += ['--include-tag', it] }
            if (excludeTags) excludeTags.each { args += ['--exclude-tag', it] }
            if (includeTests) includeTests.each { args += ['--include-classname', it] }
            if (excludeTests) excludeTests.each { args += ['--exclude-classname', it] }
            if (reportsdir) {
                if (reportsdir.exists() && !reportsdir.isDirectory()) {
                    throw new IllegalStateException("reportsdir must be a directory. $reportsdir.absolutePath")
                }
                args += ['--reports-dir', reportsdir.absolutePath]
            }
    
            if (!scanclasspath) {
                args += ['--scan-class-path']
            } else {
                scanclasspath.each { args += ['--scan-class-path', it] }
            }
        }
    }
    

    脚本片段

    task particularTests(type: SQJUnit5, dependsOn: build) {
        classpath = project.sourceSets.test.runtimeClasspath + fileTree(dir: '../../libs/junit5', include: '*.jar')
    
        excludeTags = ['DebugRun']// optional param
        includeTests = ['^.*Check$', '^.*Probe$']// optional param
        details = SQJUnit5.Details.verbose // optional param
        reportsdir = file('build/testReportDir') // optional param
    }
    

    现在,junit5测试可以用作通常的Gradle任务 .

相关问题