Search code examples
javagradlegroovyspock

Gradle: How to change test result output


By default, when executing gradle test, the output looks as such:

ExampleSpec > get all examples PASSED

If I remember correctly, back in the early days of Gradle the class would have the package name prepended. Hence you'd get a result more akin to:

com.company.ExampleSpec > get all examples PASSED

Is there any way to change how Gradle logs the finished tests to show the package name for each?


Solution

  • Nearest I could find was the following alteration to the gradle Test task:

    String ANSI_RESET = "\u001B[0m"
    String ANSI_RED = "\u001B[31m"
    String ANSI_GREEN = "\u001B[32m"
    String ANSI_YELLOW = "\u001B[33m"
    
    test {
        useJUnitPlatform()
        testLogging {
            events "failed"
            showExceptions true
            exceptionFormat "full"
        }
        afterTest { TestDescriptor descriptor, TestResult result ->
            if(result.failedTestCount > 0) {
                println "${descriptor.className} > ${descriptor.name} ${ANSI_RED}FAILED${ANSI_RESET}"
            } else if(result.skippedTestCount > 0) {
                println "${descriptor.className} > ${descriptor.name} ${ANSI_YELLOW}SKIPPED${ANSI_RESET}"
            } else {
                println "${descriptor.className} > ${descriptor.name} ${ANSI_GREEN}PASSED${ANSI_RESET}"
            }
        }
    }
    

    It results in a slight bit of redundancy in the event of a test failure, but that way it will still emit a stack trace to assist the developer.