Skip to content

Instantly share code, notes, and snippets.

@serpro69
Last active November 13, 2024 16:26
Show Gist options
  • Save serpro69/c987a4016fb59f6ca2ff9f8d8464561d to your computer and use it in GitHub Desktop.
Save serpro69/c987a4016fb59f6ca2ff9f8d8464561d to your computer and use it in GitHub Desktop.
gradle junit test output
import org.gradle.api.tasks.testing.TestResult.ResultType
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
//...
tasks.test {
useJUnitPlatform()
maxParallelForks = 1
testLogging { // credits: https://stackoverflow.com/a/36130467/5917497
// set options for log level LIFECYCLE
events = setOf(
TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_OUT
)
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
// set options for log level DEBUG and INFO
debug {
events = setOf(
TestLogEvent.STARTED,
TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT
)
exceptionFormat = TestExceptionFormat.FULL
}
info.events = debug.events
info.exceptionFormat = debug.exceptionFormat
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
if (desc.parent == null) { // will match the outermost suite
val pass = "${Color.GREEN}${result.successfulTestCount} passed${Color.NONE}"
val fail = "${Color.RED}${result.failedTestCount} failed${Color.NONE}"
val skip = "${Color.YELLOW}${result.skippedTestCount} skipped${Color.NONE}"
val type = when (val r = result.resultType) {
ResultType.SUCCESS -> "${Color.GREEN}$r${Color.NONE}"
ResultType.FAILURE -> "${Color.RED}$r${Color.NONE}"
ResultType.SKIPPED -> "${Color.YELLOW}$r${Color.NONE}"
}
val output = "Results: $type (${result.testCount} tests, $pass, $fail, $skip)"
val startItem = "| "
val endItem = " |"
val repeatLength = startItem.length + output.length + endItem.length - 36
println("")
println("\n" + ("-" * repeatLength) + "\n" + startItem + output + endItem + "\n" + ("-" * repeatLength))
}
}))
}
onOutput(KotlinClosure2({ _: TestDescriptor, event: TestOutputEvent ->
if (event.destination == TestOutputEvent.Destination.StdOut) {
logger.lifecycle(event.message.replace(Regex("""\s+$"""), ""))
}
}))
}
operator fun String.times(x: Int): String {
return List(x) { this }.joinToString("")
}
internal enum class Color(ansiCode: Int) {
NONE(0),
BLACK(30),
RED(31),
GREEN(32),
YELLOW(33),
BLUE(34),
PURPLE(35),
CYAN(36),
WHITE(37);
private val ansiString: String = "\u001B[${ansiCode}m"
override fun toString(): String {
return ansiString
}
}
@gbirchmeier
Copy link

This only shows the results if there is at least one failure. Is there a way to make it always print the results, even if the results are all passing?

@serpro69
Copy link
Author

serpro69 commented Nov 5, 2024

This only shows the results if there is at least one failure. Is there a way to make it always print the results, even if the results are all passing?

Hi @gbirchmeier ,
I'm pretty sure it can be because I remember at one point I had passed results showing, which for me was very annoying and I was looking for a way to disabled that 😁 It's probably default gradle behavior to not show test output when all tests have passed, but I'm not really sure about how to change it.
You might also want to look at https://plugins.gradle.org/plugin/com.adarshr.test-logger .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment