
概要
Gradle で実行したテスト結果は HTML 形式のテストレポートに記載され、デフォルトではコンソール出力に有用な情報がほとんど出力されません。
開発時もそうですが、CI のログが役に立ちません。
以下のようなコンソール出力になります。
> Task :app:test
AppTest > main() FAILED
org.opentest4j.AssertionFailedError at AppTest.java:13
2 tests completed, 1 failed
> Task :app:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:test'.
> There were failing tests. See the report at: file:///xxx/build/reports/tests/test/index.html
* Try:
> Run with --scan to get full insights.
BUILD FAILED in 7s
通常は以下のようにしておくのが良いでしょう。
tasks.named<Test>("test") { useJUnitPlatform() testLogging { exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL showStandardStreams = true } }
以下のようにテストの例外が出力されるようになります。
> Task :app:test
AppTest > main() FAILED
org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:531)
at app//org.example.AppTest.main(AppTest.java:13)
2 tests completed, 1 failed
> Task :app:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:test'.
> There were failing tests. See the report at: file:///xxx/app/build/reports/tests/test/index.html
* Try:
> Run with --scan to get full insights.
BUILD FAILED in 7s
TestLogging
testLogging には、ログレベル別に設定を定義できます。
debug info lifecycle warn quiet error があり、 ./gradlew test --info の場合の設定だけ変更するには以下のように書き、testLogging 直下に記載した設定を上書き定義する形となります。
tasks.named<Test>("test") {
useJUnitPlatform()
testLogging {
info {
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.SHORT
}
}
}
testLogging の設定は、ログレベル別にそれぞれのデフォルト値が設定されており、例えば --debug だと、TestExceptionFormat.FULL ですし、その他後述の設定項目が全て詳細表示されます。
通常は lifecycle しか使わないと思うので、設定する機会は少ないでしょう。
showExceptions / showStackTraces
失敗したテストの例外出力方法を設定します。
| プロパティ名 | 型 | 説明 |
|---|---|---|
| showExceptions | Boolean | テスト実行中に発生した例外(テストの失敗)をログに記録するかどうかを指定。デフォルトは全てのログレベルで true |
| showCauses | Boolean | テスト実行中に発生した例外の原因をログに記録するかどうかを指定します(showExceptionsがtrueの場合にのみ意味をなす)。デフォルト値は全てのログレベルで true |
| showStackTraces | Boolean | テスト実行中に発生した例外のスタックトレースをログに記録するかどうかを指定。デフォルトは全てのログレベルで true |
| exceptionFormat | TestExceptionFormat | showStackTraces が true の場合の出力フォーマット(TestExceptionFormat.SHORT | TestExceptionFormat.FULL)。 デフォルトは、debugおよび infoで FULL、lifecycle で SHORT。 |
showExceptions showCauses showStackTraces はデフォルトで true なので、通常は exceptionFormat のみを指定します。
lifecycle でテスト失敗のスタックトレースを出力するには以下のように設定します。
tasks.named<Test>("test") {
useJUnitPlatform()
testLogging {
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
showStandardStreams
System.out や System.err をログ出力したい場合は以下のプロパティを設定します。
| プロパティ名 | 型 | 説明 |
|---|---|---|
| showStandardStreams | Boolean | System.out (標準出力) と System.err (標準エラー) をログに記録するか。デフォルトは、debugおよび infoで true |
System.out や System.err を使うことは少ないですが、出力されなくて設定を探すのであれば、最初から true に設定しておくのがお勧めです。
tasks.named<Test>("test") { useJUnitPlatform() testLogging { showStandardStreams = true } }
この設定を true にすることは、後述の events に TestLogEvent.STANDARD_OUT と TestLogEvent.STANDARD_ERROR を設定することと同義です。
events
テストの実行過程のイベントをログ出力します。
| プロパティ名 | 型 | 説明 |
|---|---|---|
| events | Set |
テスト実行中にログ出力するイベント。lifecycle の場合は TestLogEvent.FAILED がデフォルト |
以下のような内容を設定できます。
tasks.named<Test>("test") {
useJUnitPlatform()
testLogging {
events(
org.gradle.api.tasks.testing.logging.TestLogEvent.STARTED,
org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED,
org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED,
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_OUT,
org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_ERROR,
)
}
}
以下のようにテストの実行過程がイベントとして出力されます。
AppTest > main() STARTED
AppTest > main() STANDARD_OUT
***
AppTest > main() FAILED
org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:531)
at app//org.example.AppTest.main(AppTest.java:13)
AppTest2 > main() STARTED
AppTest2 > main() PASSED
通常は特に設定は不要でしょう。設定するとしても、TestLogEvent.SKIPPED ぐらいかと思います。
その他の設定
その他、以下のような設定もありますが、変更するケースは少ないでしょう。
| プロパティ名 | 型 | 説明 |
|---|---|---|
| displayGranularity | Int | ログ記録対象イベントの表示粒度。例えば、0に設定するとメソッドレベルのイベントは「Test Run > Test Worker x > org.SomeClass > org.someMethod」と表示される。2に設定すると、同じイベントは「org.someClass > org.someMethod」と表示される。デフォルトは 2 |
| minGranularity | Int | ログに記録されるイベントの最小粒度。通常、0はテスト実行全体に対するGradle生成テストスイートのイベント、1は特定のテストJVMに対するGradle生成テストスイートのイベント、2はテストクラス、3はテストメソッドに対応。指定された粒度より低いレベルのイベントは無視される。デフォルトは -1 |
| maxGranularity | Int | ログに記録されるイベントの最大粒度。通常、0はテスト実行全体に対するGradle生成のテストスイート、1は特定のテストJVMに対するGradle生成のテストスイート、2はテストクラス、3はテストメソッドに対応。指定した粒度より高いレベルのイベントは無視される。デフォルトは-1 |
| stackTraceFilters | Set |
スタックトレースから除外するパッケージ等のフィルタ。デフォルトでGroovyやGradle内部の冗長な呼び出しが省略される。 |