Last active
November 13, 2024 16:26
-
-
Save serpro69/c987a4016fb59f6ca2ff9f8d8464561d to your computer and use it in GitHub Desktop.
gradle junit test output
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 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 | |
} | |
} |
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
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?