2017-04-20 4 views
0

Gradleで実行する予定のjunit5にテストを移動しています。私が働いているプロジェクトでは、単体テストと、必要に応じて実行しなければならない特定のテスト(私が想定している特定のGradleタスク)があります。Gradleを使用してJunit5テストを実行する方法(ビルドサイクルから外れる)

ユニットテストについてはっきりしています。 Gradle Pluginはそれをサポートします。 しかし私は自分のニーズに合わせて別のテストタスクを定義する方法を見つけることができませんでした Junit5プラグインのソースを検索し、その目的のための特別なクラスがないことを発見しました。 Gradleプラグインは単にJavaExecタスクを設定して実行します。 ここでは、タスク

を設定します。

従って、この

タスクMYTASK(Junit5TestRunnerタイプ)のような組み込み型 の自分のタスクを定義するには、目に見える方法がないように見えます

どうすればいいのでしょうか?

答えて

2

新しい設定を定義し、junit-platform-console-standalone artifactに依存し、必要に応じてコンソールランチャーを設定します。同様に:

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)のために少しよりよい意思決定を発見したように思えるここでは、タスクを設定

ソルムラスの答えは私に確かに正しい方向を与えた 解決策最も定型的なコードを別のタスククラスに移動し、スクリプトからそのタスクを使用して再利用可能にすることです。

クラス

/** 
* 
* 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のタスクとして使用することができますスニペット。

関連する問題