1

Running: ./gradlew test triggers the test run successfully:

Task :test FAILED

PerfectNumberTest > Test calcPerfectNumbers() method FAILED
    org.opentest4j.AssertionFailedError at PerfectNumberTest.java:51

5 tests completed, 1 failed

However, I do not get the output which would indicate what exactly went wrong in the test,

For instance: expected 1 instead of 2

These are the dependencies which I'm using:

dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}
EugenSunic
  • 13,162
  • 13
  • 64
  • 86

2 Answers2

3

Use the following configuration to your build.gradle file:

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.STARTED,
                   TestLogEvent.FAILED,
                   TestLogEvent.PASSED,
                   TestLogEvent.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces true
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                def duration = String.format("%.3f sec", (result.endTime - result.startTime) / 1000)
                def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped) ${duration}"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
            }
        }
    }
}

This configuration will show everything even the console outuput

See this post question for more details Gradle: How to Display Test Results in the Console in Real Time?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Youans
  • 4,801
  • 1
  • 31
  • 57
1

Here is a modified version of the configuration that Youans shared:

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.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces false
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        beforeSuite { desc ->
            if (!desc.name.startsWith("Gradle Test")) { 
                println "\nRunning ${desc.name}"
            }
        }

        afterSuite { desc, result ->
            def duration = String.format("%.3f sec", (result.endTime - result.startTime) / 1000)

            if (desc.name.startsWith("Gradle Test Executor")) {
                def output = "Result: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped) ${duration}"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
                return
            }

            if (!desc.name.startsWith("Gradle Test")) { 
                println "Tests run: ${result.testCount}, Failures: ${result.failedTestCount}, Errors: 0, Skipped: ${result.skippedTestCount} Time elapsed: ${duration}"
            }
        }
    }
}

Modifications:

  • Changed the suite detection to account for name patterns
  • Removed TestLogEvent.STARTED and TestLogEvent.PASSED from events
  • Changed showStackTraces to false

It prints the following minimal output:

Running gradle.example.FooTest

FooTest > someLibraryMethod() FAILED
    org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0 Time elapsed: 0.045 sec

Running gradle.example.LibraryTest

LibraryTest > someLibraryMethodReturnsTrue() FAILED
    org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 Time elapsed: 0.006 sec

------------------------------------------------------------------------
|  Result: FAILURE (3 tests, 1 passed, 2 failed, 0 skipped) 0.331 sec  |
------------------------------------------------------------------------

3 tests completed, 2 failed

Of course the stack trace should probably be kept, but this will at least show the expected/actual values at a minimum.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132