Last active
November 4, 2024 23:00
-
-
Save lwasyl/f5b2b4ebe9e348ebbd8ee4cb995f8362 to your computer and use it in GitHub Desktop.
Gradle tests logging #loggingpost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.time.TimeCategory | |
import org.gradle.api.tasks.testing.logging.TestExceptionFormat | |
import org.gradle.api.tasks.testing.logging.TestLogEvent | |
rootProject { | |
ext.testsResults = [] // Container for tests summaries | |
allprojects { project -> | |
tasks.withType(Test) { testTask -> | |
testTask.testLogging { logging -> | |
events TestLogEvent.FAILED, | |
TestLogEvent.SKIPPED, | |
TestLogEvent.STANDARD_OUT, | |
TestLogEvent.STANDARD_ERROR | |
exceptionFormat TestExceptionFormat.FULL | |
showExceptions true | |
showCauses true | |
showStackTraces true | |
} | |
ignoreFailures = true // Always try to run all tests for all modules | |
afterSuite { desc, result -> | |
if (desc.parent) return // Only summarize results for whole modules | |
String summary = "${testTask.project.name}:${testTask.name} results: ${result.resultType} " + | |
"(" + | |
"${result.testCount} tests, " + | |
"${result.successfulTestCount} successes, " + | |
"${result.failedTestCount} failures, " + | |
"${result.skippedTestCount} skipped" + | |
") " + | |
"in ${TimeCategory.minus(new Date(result.endTime), new Date(result.startTime))}" + | |
"\n" + | |
"Report file: ${testTask.reports.html.entryPoint}" | |
// Add reports in `testsResults`, keep failed suites at the end | |
if (result.resultType == TestResult.ResultType.SUCCESS) { | |
rootProject.testsResults.add(0, summary) | |
} else { | |
rootProject.testsResults += summary | |
} | |
} | |
} | |
} | |
} | |
gradle.buildFinished { | |
def allResults = rootProject.ext.testsResults | |
if (!allResults.isEmpty()) { | |
printResults allResults | |
} | |
} | |
private static void printResults(allResults) { | |
def maxLength = allResults*.readLines().flatten().collect { it.length() }.max() | |
println "┌${"${"─" * maxLength}"}┐" | |
println allResults.collect { | |
it.readLines().collect { | |
"│" + it + " " * (maxLength - it.length()) + "│" | |
}.join("\n") | |
}.join("\n├${"${"─" * maxLength}"}┤\n") | |
println "└${"${"─" * maxLength}"}┘" | |
} |
I've converted this script to kotlin: https://gist.github.com/ethanmdavidson/a73147ce5bdcde4a87554c7303bae8f4
How can I use this script for a single project? Just include it in the root build.gradle
file?
gradle.buildFinished
is now deprecated. Confoundingly, it seems there is no clean replacement. Anyone figure it out?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the article and sample code. It's great!
https://medium.com/@wasyl/pretty-tests-summary-in-gradle-744804dd676c