spekframework / spek

A specification framework for Kotlin
Other
2.23k stars 180 forks source link

Report better test case names on failures or provide a mechanism to fetch the full path name of the test #639

Open dibog opened 5 years ago

dibog commented 5 years ago

If a test case fails the reports are not very descriptive.

E.g. take:

class CalculatorSpec : Spek({
    describe("Calculator") {
        val calculator by memoized { Calculator()}

        listOf(
                TestArg( 1, 2, 3+1 ), // <- to provoke the test cases to fail
                TestArg( 2, 3, 5+1 ),
                TestArg( 4, 5, 9+1 )
        ).forEach { ( a, b, expectedSum ) ->
            context("adding $a and $b") {
                val actualSum = calculator.add(a, b)

                it("should be $expectedSum") {
                    assertThat( actualSum ).isEqualTo( expectedSum )
                }
            }
        }
    }
})

which will report those failures as

[ERROR]   CalculatorSpec expected:<[10]> but was:<[9]>
[ERROR]   CalculatorSpec expected:<[4]> but was:<[3]>
[ERROR]   CalculatorSpec expected:<[6]> but was:<[5]>

The message would be better if we can provide the path name of the test. Unfortunately this path name can't be accessed currently. With the following, very ugly extension function:

fun TestBody.fullPathName(): String? {
    val testScope = memoized<TestScope>() as? MemoizedValueReader<TestScope>
    return testScope?.scope?.path.toString()
}

and the assertion in the original code changed as the following:

assertThat( actualSum, fullPathName() ).isEqualTo( expectedSum )

we get a better report:

[ERROR]   CalculatorSpec expected [test/CalculatorSpec/Calculator/adding 4 and 5/should be 10]:<[10]> but was:<[9]>
[ERROR]   CalculatorSpec expected [test/CalculatorSpec/Calculator/adding 1 and 2/should be 4]:<[4]> but was:<[3]>
[ERROR]   CalculatorSpec expected [test/CalculatorSpec/Calculator/adding 2 and 3/should be 6]:<[6]> but was:<[5]>

This is IMHO more readable.

So please either let the report generate a better description or add the fullPathName() method to the TestBody.

raniejade commented 5 years ago

IMHO generating more descriptive reports is the way to go. Starting 2.1.0 as part of Kotlin Native support - Spek will provide its own gradle plugin which will have proper reporting (similar to rspec or jasmine).

dibog commented 5 years ago

Please don't forget to add it to maven, too, as not all the world is using gradle or is thinking about to migrate to gradle.

dibog commented 5 years ago

I added a pull request for this issue: https://github.com/spekframework/spek/pull/641

raniejade commented 5 years ago

Please don't forget to add it to maven, too, as not all the world is using gradle or is thinking about to migrate to gradle.

The maven plugin for kotlin is getting left out in terms of features compared to the gradle plugin. Correct me if I'm wrong, I can't see any docs to setup mpp projects using maven.

dibog commented 5 years ago

mpp as in multi platform projects? Tbh. I'm using kotlin for server centered development. So I'm interested in using Spek there and we are using maven for building our projects. The current maven support of Spek is fine for me, at least with the support mentioned in this issue. So, please don't drop the maven plugin you have.

joaolrpaulo commented 3 years ago

👋 Any news on support for this feature?