https://kotlinlang.org logo
#kotest
Title
# kotest
m

Michael Strasser

11/18/2021, 8:32 AM
I am trying to use
withData
and
DescribeSpec
but I can’t get the tests to be named as I expect. Given a function
fun messageMap(id: String): Map<String, Message>
that returns 3 different messages, if I use:
Copy code
describe("adds messages") {
    val id = randomId()
    it("sets ID from message") {
        withData(messageMap(id)) { message ->
            Builder.newInstance().addMessage(message).getId() shouldBe id
        }
    }
}
The test runs once with title “sets ID from message”. But if I use:
Copy code
describe("adds messages") {
    val id = randomId()
    withData(messageMap(id)) { message ->
        it("sets ID from message") {
            Builder.newInstance().addMessage(message).getId() shouldBe id
        }
    }
}
The test runs 3 times with titles:
Copy code
sets ID from message
(1) sets ID from message
(2) sets ID from message
I expected from the docs that the string keys for the map would be used as test names. I am using Kotest 4.6.3.
b

BorzdeG

11/20/2021, 11:18 AM
Copy code
describe("adds messages") {
    it("sets ID from message") {
        val id = randomId()
        withData(messageMap(id)) { message ->
            Builder.newInstance().addMessage(message).getId() shouldBe id
        }
    }
}
s

sam

11/20/2021, 4:09 PM
Note that in 5.0 you can only nest withData inside parent tests (so not
it
). Otherwise the output doesn't work properly on gradle.
b

BorzdeG

11/20/2021, 4:11 PM
hm. its works me on 5.0.0.RC
s

sam

11/20/2021, 4:11 PM
it does ??
lol
ahh it's probably binding to the outer scope
I'll have another attempt this weekend to see if we can relax that restriction before 5.0.0 final, but it's gradle not respecting the junit spec properly causing issues.
m

Michael Strasser

11/21/2021, 11:39 AM
It does work as expected when I use the right combination of functions: inside
describe
but without specifying
it
. For example:
Copy code
describe("adds messages") {
    describe("sets ID from message") {
        withData(messageMap(randomId())) { message ->
            Builder.newInstance().addMessage(message).getId() shouldBe id
        }
    }
}
Run by IntelliJ but not run by Gradle. Sigh
s

sam

11/21/2021, 2:27 PM
What happens with Gradle ?
m

Michael Strasser

11/21/2021, 10:08 PM
Gradle does not run the tests at all. Tests specified using
it
run, but those generated by
withData
do not. By contrast, the Kotest plugin runs all of them. In one test file, Gradle and Kotest both report 9 tests (the number of `it`s) but Kotest runs the 92 extra
withData
tests, while Gradle does not (as far as I can tell). I ran a small experiment with that file. I caused a test to fail by modifying a value returned by the
messageMap()
function. • Kotest reported details of the failing test, as expected. • Gradle reported failure of the parent
describe
block but with no details. I hope this helps. (Sorry, I can’t share actual client code with you.)
s

sam

11/21/2021, 10:09 PM
Ok I can take a guess as to the issue. The tests are not nesting properly in 4.6.x with describe is my guess.
There's a bug in gradle where it doesn't adhere to the JUnit spec properly. 5.0 has a workaround
m

Michael Strasser

11/21/2021, 10:22 PM
I switched to Kotest 5.0.0.RC. Both Gradle and Kotest runners now report 101 tests for the file. When I change
messageMap()
to break tests, both Gradle and Kotest correctly report which tests failed.
s

sam

11/21/2021, 10:22 PM
ok cool
m

Michael Strasser

11/21/2021, 10:23 PM
Thanks for your help!
s

sam

11/21/2021, 10:23 PM
You're welcome
Just don't nest tests inside
it
as that will be disallowed later
leave them in the describe blocks
m

Michael Strasser

11/21/2021, 10:25 PM
Yep, I understand now.
108 Views