Test report reworked (#522) by nikitavlaev · Pull Request #523 · UnitTestBot/UTBotJava · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,29 @@ data class TestsGenerationReport(
get() = executables.firstOrNull()?.clazz
?: error("No executables found in test report")

// Summary message is generated lazily to avoid evaluation of classUnderTest
var summaryMessage: () -> String = { "Unit tests for $classUnderTest were generated successfully." }
val initialWarnings: MutableList<() -> String> = mutableListOf()
val hasWarnings: Boolean
get() = initialWarnings.isNotEmpty()

val detailedStatistics: String
get() = buildString {
appendHtmlLine("Class: ${classUnderTest.qualifiedName}")
val testMethodsStatistic = executables.map { it.countTestMethods() }
val errors = executables.map { it.countErrors() }
val overallErrors = errors.sum()

appendHtmlLine("Successful test methods: ${testMethodsStatistic.sumBy { it.successful }}")
appendHtmlLine(
"Failing because of unexpected exception test methods: ${testMethodsStatistic.sumBy { it.failing }}"
)
appendHtmlLine(
"Failing because of exceeding timeout test methods: ${testMethodsStatistic.sumBy { it.timeout }}"
)
appendHtmlLine(
"Failing because of possible JVM crash test methods: ${testMethodsStatistic.sumBy { it.crashes }}"
)
appendHtmlLine("Not generated because of internal errors test methods: $overallErrors")
}

fun addMethodErrors(testCase: UtTestCase, errors: Map<String, Int>) {
this.errors[testCase.method] = errors
Expand All @@ -216,61 +236,24 @@ data class TestsGenerationReport(
}
}

override fun toString(): String = buildString {
appendHtmlLine(summaryMessage())
appendHtmlLine()
initialWarnings.forEach { appendHtmlLine(it()) }
appendHtmlLine()
fun toString(isShort: Boolean): String = buildString {
appendHtmlLine("Target: ${classUnderTest.qualifiedName}")
if (initialWarnings.isNotEmpty()) {
initialWarnings.forEach { appendHtmlLine(it()) }
appendHtmlLine()
}

val testMethodsStatistic = executables.map { it.countTestMethods() }
val errors = executables.map { it.countErrors() }
val overallTestMethods = testMethodsStatistic.sumBy { it.count }
val overallErrors = errors.sum()

appendHtmlLine("Overall test methods: $overallTestMethods")
appendHtmlLine("Successful test methods: ${testMethodsStatistic.sumBy { it.successful }}")
appendHtmlLine(
"Failing because of unexpected exception test methods: ${testMethodsStatistic.sumBy { it.failing }}"
)
appendHtmlLine(
"Failing because of exceeding timeout test methods: ${testMethodsStatistic.sumBy { it.timeout }}"
)
appendHtmlLine(
"Failing because of possible JVM crash test methods: ${testMethodsStatistic.sumBy { it.crashes }}"
)
appendHtmlLine("Not generated because of internal errors test methods: $overallErrors")
}

// TODO: should we use TsvWriter from univocity instead of this manual implementation?
fun getFileContent(): String =
(listOf(getHeader()) + getLines()).joinToString(System.lineSeparator())

private fun getHeader(): String {
val columnNames = listOf(
"Executable/Number of test methods",
SUCCESSFUL,
FAILING,
TIMEOUT,
CRASH,
"Errors tests"
)

return columnNames.joinToString(TAB_SEPARATOR)
if (!isShort) {
appendHtmlLine(detailedStatistics)
}
}

private fun getLines(): List<String> =
executables.map { executable ->
val testMethodStatistic = executable.countTestMethods()
with(testMethodStatistic) {
listOf(
executable,
successful,
failing,
timeout,
crashes,
executable.countErrors()
).joinToString(TAB_SEPARATOR)
}
}
override fun toString(): String = toString(false)

private fun UtMethod<*>.countTestMethods(): TestMethodStatistic = TestMethodStatistic(
testMethodsNumber(successfulExecutions),
Expand All @@ -291,9 +274,4 @@ data class TestsGenerationReport(
private data class TestMethodStatistic(val successful: Int, val failing: Int, val timeout: Int, val crashes: Int) {
val count: Int = successful + failing + timeout + crashes
}

companion object {
private const val TAB_SEPARATOR: String = "\t"
const val EXTENSION: String = ".tsv"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class GenerateTestsAndSarifReportFacade(
mergedSarifReportFile.writeText(mergedReport)
if (verbose) {
println("SARIF report was saved to \"${mergedSarifReportFile.path}\"")
println("You can open it using the VS Code extension \"Sarif Viewer\"")
}
}
}
Expand Down
Loading