Skip to content

Commit

Permalink
refactor: Add removing stack traces for simple report
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed May 13, 2021
1 parent 896d64b commit 5bac6e9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package ftl.domain.junit

import ftl.api.JUnitTest

fun JUnitTest.Result.removeStackTraces() = JUnitTest.Result(testsuites?.removeStackTraces())
fun JUnitTest.Result.removeStackTraces() = copy(testsuites = testsuites?.removeStackTraces())

private fun MutableCollection<JUnitTest.Suite>.removeStackTraces() = map { testSuite ->
testSuite.copy(testcases = testSuite.testcases?.withRemovedStackTraces()?.toMutableList())
testSuite.copy(testcases = testSuite.testcases?.withRemovedStackTraces())
}.toMutableList()

private fun MutableCollection<JUnitTest.Case>.withRemovedStackTraces() =
map { testCase -> testCase.removeFlakyStackTrace() }
map { testCase -> testCase.removeFlakyStackTrace() }.toMutableList()

private fun JUnitTest.Case.removeFlakyStackTrace(): JUnitTest.Case = if (flaky == true) {
copy(
errors = null,
failures = null
)
} else this
private fun JUnitTest.Case.removeFlakyStackTrace(): JUnitTest.Case =
if (flaky == true) copy(errors = null, failures = null) else this
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ftl.domain.junit

import com.google.common.truth.Truth.assertThat
import ftl.api.JUnitTest
import org.junit.Test

class StackTraceRemoverTest {

val testcases = mutableListOf(
JUnitTest.Case("name", "class", "time", failures = listOf("fail"), errors = listOf("error")),
JUnitTest.Case("name2", "class2", "time2", failures = listOf("fail2"), errors = listOf("error2")),
)

@Test
fun `Should remove stack traces for flaky tests`() {
// given
val testcases = mutableListOf(
JUnitTest.Case("name", "class", "time", failures = listOf("fail"), errors = listOf("error")),
JUnitTest.Case("name2", "class2", "time2", failures = listOf("fail2"), errors = listOf("error2")),
)
val junitTestResult = testResultsFor(testcases, flaky = true)

// when
val actual = junitTestResult.removeStackTraces()

// then
actual.testsuites?.forEach { suite ->
assertThat(suite.testcases?.all { testCase -> testCase.failures == null }).isTrue()
assertThat(suite.testcases?.all { testCase -> testCase.errors == null }).isTrue()
}
}

@Test
fun `Should not remove stack traces for not flaky test`() {
// given
val junitTestResult = testResultsFor(testcases, flaky = false)

// when
val actual = junitTestResult.removeStackTraces()

// then
actual.testsuites?.forEach { suite ->
assertThat(suite.testcases?.all { testCase -> testCase.failures == null }).isFalse()
assertThat(suite.testcases?.all { testCase -> testCase.errors == null }).isFalse()
}
}

private fun testResultsFor(testCases: MutableList<JUnitTest.Case>, flaky: Boolean) = JUnitTest.Result(
testsuites = mutableListOf(
JUnitTest.Suite(
"",
"-1",
"-1",
-1,
"-1",
"-1",
"-1",
"-1",
"-1",
"-1",
testCases.onEach { it.apply { this.flaky = flaky } },
null,
null,
null
)
)
)
}

0 comments on commit 5bac6e9

Please sign in to comment.