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

时间:2017-04-20 06:48:37

标签: gradle junit5

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

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

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

  

任务myTask(类型:Junit5TestRunner)     在这里我们设置一个任务

有什么想法可以做到吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

似乎我找到了更好的决定

  
    

任务myTask(类型:Junit5TestRunner)这里我们设置一个任务

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

班级

/**
 *
 * 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任务。