I am trying to use `withData` and `DescribeSpec` b...
# kotest
m
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
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
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
hm. its works me on 5.0.0.RC
s
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
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
What happens with Gradle ?
m
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
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
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
ok cool
m
Thanks for your help!
s
You're welcome
Just don't nest tests inside
it
as that will be disallowed later
leave them in the describe blocks
m
Yep, I understand now.
120 Views