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

Matteo Mirk

12/06/2021, 9:12 AM
Is it possible to write a data driven test using StringSpec, wrapping it in a context? So far I’ve only been able to make it work only like this:
Copy code
class PriceSpec : StringSpec({
    withData(
        nameFn = { "goods=${it.goods}" },
        // data rows
    ) { (goods, total) ->
        price(goods) shouldBe total
    }

    "test incremental" {
        ...
    }
})
Originally there were 2 tests, then I transformed the first into a ddt, but I lost its name wrapping the generated cases. I would like to give a name to the ddt, but If I put the withData() declaration inside a string block it’ll fail at runtime, and if I wrap it with a context(“name”) as shown in the docs, only the second test will be executed. What am I doing wrong?
Ok, I was able to achieve it using FreeSpec!
s

sam

12/06/2021, 5:03 PM
FreeSpec is just String spec that allows parents basically
StringSpec prob shouldn't exist
m

Matteo Mirk

12/06/2021, 5:07 PM
Thanks, realized it only after reading some finer details about withData() usage: it can’t be used inside leaf blocks, which are those provided by StringSpec. So for whomever is reading: I made it work with FreeSpec like this:
Copy code
"test totals" - { // container block -> OK
    withData(
        nameFn = { "goods=${it.goods}" },
        // data rows
    ) { (goods, total) ->
        price(goods) shouldBe total
    }
}

"test incremental" { // leaf block, same as in StringSpec
    ...
}
s

sam

12/06/2021, 5:29 PM
Yep that's exactly right
👍 1
191 Views