グラドル:テスト結果をリアルタイムでコンソールに表示するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/3963708

  •  09-10-2019
  •  | 
  •  

質問

テスト結果(system.out/err、テスト中のコンポーネントからのログメッセージ)を見たいです。 彼らが走りながら 同じコンソールで次のように実行します。

gradle test

そして、テストが完了するまで待たずにテストレポートを確認してください(テストレポートはテストが完了したときにのみ生成されるため、テストの実行中に何も「tail -f」することはできません)。

役に立ちましたか?

解決

コマンドラインで情報ロギングレベルでGradleを実行できます。実行中の各テストの結果が表示されます。欠点は、他のタスクでもはるかに多くの出力を得ることです。

gradle test -i

他のヒント

これが私の派手なバージョンです:

fancy test result

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

tasks.withType(Test) {
    testLogging {
        // set options for log level LIFECYCLE
        events TestLogEvent.FAILED,
               TestLogEvent.PASSED,
               TestLogEvent.SKIPPED,
               TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces true

        // set options for log level DEBUG and INFO
        debug {
            events TestLogEvent.STARTED,
                   TestLogEvent.FAILED,
                   TestLogEvent.PASSED,
                   TestLogEvent.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
            exceptionFormat TestExceptionFormat.FULL
        }
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
            }
        }
    }
}

build.gradleファイル内にグルーヴィーな閉鎖を追加することができます。

test {
    afterTest { desc, result -> 
        logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
}

コンソールでは、次のように読み取ります。

:compileJava UP-TO-DATE
:compileGroovy
:processResources
:classes
:jar
:assemble
:compileTestJava
:compileTestGroovy
:processTestResources
:testClasses
:test
Executing test maturesShouldBeCharged11DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
Executing test studentsShouldBeCharged8DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
Executing test seniorsShouldBeCharged6DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
Executing test childrenShouldBeCharged5DollarsAnd50CentForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
:check
:build

バージョン1.1のGradleは大いにサポートしているためです テスト出力を記録するためのその他のオプション. 。これらのオプションを手元に置いて、次の構成で同様の出力を実現できます。

test {
    testLogging {
        events "passed", "skipped", "failed"
    }
}

として ステファングラーゼ 回答:

次のコードをあなたに追加します build.gradle (バージョン1.1なので)出力のために正常に動作します 合格した, スキップ失敗した テスト。

test {
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

私がさらに言いたいのは、これがスターターにとって問題だとわかった)ということです。 gradle test コマンドはテストを実行します 変更ごとに1回だけ.

だからあなたがそれを実行しているなら 2回目はテスト結果に出力がありません. 。建物の出力でもこれを見ることができます:Gradleは言います 最新の テストで。したがって、N-3番目の時間は実行されませんでした。

スマートグレード!

テストケースを強制的に実行する場合は、使用してください gradle cleanTest test.

これは少し話題から外れていますが、初心者に役立つことを願っています。

編集

として SPARC_SPREAD コメントに記載されています:

Gradleを強制したい場合 常に新鮮なテストを実行してください (これは必ずしも良い考えではないかもしれません)あなたは追加することができます outputs.upToDateWhen {false}testLogging { [...] }. 。読み続けてください ここ.

平和。

免責事項:私はGradle Test Loggerプラグインの開発者です。

単に使用できます Gradle Test Loggerプラグイン コンソールに美しいログを印刷します。プラグインは、構成がほとんどまたはまったくないほとんどのユーザーを満たすために賢明なデフォルトを使用しますが、すべての人に合わせて多くのテーマと構成オプションを提供します。

注:Gradle Test LoggerプラグインV1.4+は、並列テストの実行もサポートするようになりました。単にaを使用します 適切なテーマ.

Standard Theme標準テーマ

Mocha Themeモカのテーマ

使用法

plugins {
    id 'com.adarshr.test-logger' version '<version>'
}

常に入手してください Gradle Centralの最新バージョン.

構成

構成はまったく必要ありません。ただし、プラグインにはいくつかのオプションがあります。これは、次のように実行できます(表示されているデフォルト値):

testlogger {
    // pick a theme - mocha, standard, plain, mocha-parallel, standard-parallel or plain-parallel
    theme 'standard'

    // set to false to disable detailed failure logs
    showExceptions true

    // set to false to hide stack traces
    showStackTraces true

    // set to true to remove any filtering applied to stack traces
    showFullStackTraces false

    // set to false to hide exception causes
    showCauses true

    // set threshold in milliseconds to highlight slow tests
    slowThreshold 2000

    // displays a breakdown of passes, failures and skips along with total duration
    showSummary true

    // set to true to see simple class names
    showSimpleNames false

    // set to false to hide passed tests
    showPassed true

    // set to false to hide skipped tests
    showSkipped true

    // set to false to hide failed tests
    showFailed true

    // enable to see standard out and error streams inline with the test results
    showStandardStreams false

    // set to false to hide passed standard out and error streams
    showPassedStandardStreams true

    // set to false to hide skipped standard out and error streams
    showSkippedStandardStreams true

    // set to false to hide failed standard out and error streams
    showFailedStandardStreams true
}

私はあなたがそれを使うことを楽しんでくれることを願っています。

これを追加します build.gradle GradleがStdoutとStderrを飲み込むのを止める。

test {
    testLogging.showStandardStreams = true
}

文書化されています ここ.

「テスト」タスクは、Androidプラグインでは機能しません。Androidプラグインは以下を使用します。

// Test Logging
tasks.withType(Test) {
    testLogging {
        events "started", "passed", "skipped", "failed"
    }
}

以下を参照してください: https://stackoverflow.com/a/31665341/3521637

フォローアップとして シュバムの素晴らしい答え 私は使用を提案するのが好きです 列挙 の代わりに値 文字列. 。を見てください TestLoggingクラスのドキュメント.

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

tasks.withType(Test) {
    testLogging {
        events TestLogEvent.FAILED,
               TestLogEvent.PASSED,
               TestLogEvent.SKIPPED,
               TestLogEvent.STANDARD_ERROR,
               TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showCauses true
        showExceptions true
        showStackTraces true
    }
}

Shubham Chaudharyの答えに基づいた私のお気に入りのミニマルなバージョン。enter image description here

これを入れてください build.gradle ファイル:

test {
    afterSuite { desc, result ->
    if (!desc.parent)
        println("${result.resultType} " +
            "(${result.testCount} tests, " +
            "${result.successfulTestCount} successes, " +
            "${result.failedTestCount} failures, " +
            "${result.skippedTestCount} skipped)")
    }
}

Androidプラグインを使用してGradleで:

gradle.projectsEvaluated {
    tasks.withType(Test) { task ->
        task.afterTest { desc, result ->
            println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
        }
    }
}

その後、出力は次のとおりです。

Test TestConversionMinutes [org.example.app.test.durationTest]を実行すると結果:成功

のマージ シュバムの素晴らしい答えjjd文字列の代わりにenumを使用します

tasks.withType(Test) {
   testLogging {
       // set options for log level LIFECYCLE
       events TestLogEvent.PASSED,
            TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT
       showExceptions true
       exceptionFormat TestExceptionFormat.FULL
       showCauses true
       showStackTraces true

    // set options for log level DEBUG and INFO
       debug {
        events TestLogEvent.STARTED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT, TestLogEvent.STANDARD_ERROR
        exceptionFormat TestExceptionFormat.FULL
       }
       info.events = debug.events
       info.exceptionFormat = debug.exceptionFormat

       afterSuite { desc, result ->
           if (!desc.parent) { // will match the outermost suite
               def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
               def startItem = '|  ', endItem = '  |'
               def repeatLength = startItem.length() + output.length() + endItem.length()
               println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
           }
       }
   }
}

から続きます ベンジャミン・ムスコの答え (2011年3月19日)、あなたは -i 旗と一緒に グレップ, 、1000の不要なラインを除外します。例:

強いフィルター - 各ユニットテスト名と結果、および全体的なビルドステータスのみを表示します。セットアップエラーまたは例外は表示されません。

./gradlew test -i | grep -E " > |BUILD"

ソフトフィルター - 各ユニットテスト名と結果、およびセットアップエラー/例外を表示します。しかし、それはいくつかの無関係な情報も含まれます:

./gradlew test -i | grep -E -v "^Executing |^Creating |^Parsing |^Using |^Merging |^Download |^title=Compiling|^AAPT|^future=|^task=|:app:|V/InstrumentationResultParser:"

ソフトフィルター、代替構文: (検索トークンは個々の文字列に分割されます)

./gradlew test -i | grep -v -e "^Executing " -e "^Creating " -e "^Parsing " -e "^Using " -e "^Merging " -e "^Download " -e "^title=Compiling" -e "^AAPT" -e "^future=" -e "^task=" -e ":app:" -e "V/InstrumentationResultParser:"

それがどのように機能するかについての説明: 最初のコマンドの出力、 ./gradlew test -i, 、2番目のコマンドに配管されます grep, 、正規表現に基づいて多くの不要な線を除外します。 "-E" 正規表現モードを有効にします "|" 「または」を意味します。ユニットテスト名と結果は使用して表示できます " > ", 、および全体のステータスが許可されます "BUILD". 。ソフトフィルターで、 "-v" フラグは意味があります 「含まれていない」"^" 「ラインの開始」を意味します。したがって、「実行」または「作成」などから始まるすべての行を取り除きます。


Gradle 5.1のAndroid計装ユニットテストの例:

./gradlew connectedDebugAndroidTest --continue -i | grep -v -e \
"^Transforming " -e "^Skipping " -e "^Cache " -e "^Performance " -e "^Creating " -e \
"^Parsing " -e "^file " -e "ddms: " -e ":app:" -e "V/InstrumentationResultParser:"

Gradle 4.10のJacoco単位テストカバレッジの例:

./gradlew createDebugCoverageReport --continue -i | grep -E -v "^Executing |^Creating |^Parsing |^Using |^Merging |^Download |^title=Compiling|^AAPT|^future=|^task=|:app:|V/InstrumentationResultParser:"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top