-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add gradle script that helps showing better output when running tests…
… using the cli
- Loading branch information
Souleymane Sidibe
committed
Aug 19, 2021
1 parent
502c375
commit f047a63
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat | ||
import org.gradle.api.tasks.testing.logging.TestLogEvent | ||
import groovy.time.TimeCategory | ||
import java.util.Date | ||
|
||
/** | ||
* based on the groovy code by lwasyl: | ||
* https://gist.github.com/lwasyl/f5b2b4ebe9e348ebbd8ee4cb995f8362 | ||
* https://medium.com/@wasyl/pretty-tests-summary-in-gradle-744804dd676c | ||
*/ | ||
var testResults by extra(mutableListOf<TestOutcome>()) // Container for tests summaries | ||
|
||
tasks.withType<Test>().configureEach { | ||
val testTask = this | ||
|
||
testLogging { | ||
events = setOf( | ||
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 | ||
|
||
//addTestListener is a workaround https://github.com/gradle/kotlin-dsl-samples/issues/836 | ||
addTestListener(object : TestListener { | ||
override fun beforeSuite(suite: TestDescriptor) {} | ||
override fun beforeTest(testDescriptor: TestDescriptor) {} | ||
override fun afterTest(testDescriptor: TestDescriptor, result: TestResult) {} | ||
override fun afterSuite(desc: TestDescriptor, result: TestResult) { | ||
if (desc.parent != null) return // Only summarize results for whole modules | ||
|
||
val summary = TestOutcome().apply { | ||
add( "${testTask.project.name}:${testTask.name} results: ${result.resultType} " + | ||
"(" + | ||
"${result.testCount} tests, " + | ||
"${result.successfulTestCount} successes, " + | ||
"${result.failedTestCount} failures, " + | ||
"${result.skippedTestCount} skipped" + | ||
") " + | ||
"in ${TimeCategory.minus(Date(result.endTime), Date(result.startTime))}") | ||
add("Report file: ${testTask.reports.html.entryPoint}") | ||
} | ||
|
||
// Add reports in `testsResults`, keep failed suites at the end | ||
if (result.resultType == TestResult.ResultType.SUCCESS) { | ||
testResults.add(0, summary) | ||
} else { | ||
testResults.add(summary) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
gradle.buildFinished { | ||
if (testResults.isNotEmpty()) { | ||
printResults(testResults) | ||
} | ||
} | ||
|
||
fun printResults(allResults:List<TestOutcome>) { | ||
val maxLength = allResults.map{ it.maxWidth() } | ||
.maxOrNull() ?: 0 | ||
|
||
println("┌${"─".repeat(maxLength)}┐") | ||
|
||
println(allResults.joinToString("├${"─".repeat(maxLength)}┤\n") { testOutcome -> | ||
testOutcome.lines.joinToString("│\n│", "│", "│") { | ||
it + " ".repeat(maxLength - it.length) | ||
} | ||
}) | ||
|
||
println("└${"─".repeat(maxLength)}┘") | ||
} | ||
|
||
data class TestOutcome(val lines:MutableList<String> = mutableListOf()) { | ||
fun add(line:String) { | ||
lines.add(line) | ||
} | ||
|
||
fun maxWidth(): Int { | ||
return lines.maxByOrNull { it.length }?.length ?: 0 | ||
} | ||
} |