2017-04-20 54 views
0

我正在将测试迁移到junit5,这些测试将与Gradle一起运行。在我使用的项目中,有单元测试和一些必须按需运行的特定测试(从特定的Gradle任务我想)。如何运行特定的(建设周期外)使用Gradle的Junit5测试

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

因此它似乎有确定自己的 任务不可见的方式内置在这样

任务myTask(类型:Junit5TestRunner)型 在这里,我们成立了专案组

任何想法如何完成?

回答

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.gradle

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

+0

好吧。谢谢,我会尝试。 –

+0

downloadJUnitPlatformStandalone的用途是什么。我可以参考当地图书馆,不要复制? –

+0

它从配置的Maven仓库下载(复制)依赖关系。 – Sormuras

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测试可以作为一个平常摇篮任务。

相关问题