Skip to content

Instantly share code, notes, and snippets.

@lwasyl
Last active November 4, 2024 23:00
Show Gist options
  • Save lwasyl/f5b2b4ebe9e348ebbd8ee4cb995f8362 to your computer and use it in GitHub Desktop.
Save lwasyl/f5b2b4ebe9e348ebbd8ee4cb995f8362 to your computer and use it in GitHub Desktop.
Gradle tests logging #loggingpost
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}"}"
}
@AndreiYu
Copy link

thanks for the article and sample code. It's great!
https://medium.com/@wasyl/pretty-tests-summary-in-gradle-744804dd676c

@ethanmdavidson
Copy link

@Kshitij09-ag
Copy link

How can I use this script for a single project? Just include it in the root build.gradle file?

@gbirchmeier
Copy link

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