Is it possible to write a data driven test using S...
# kotest
m
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
FreeSpec is just String spec that allows parents basically
StringSpec prob shouldn't exist
m
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
    ...
}
👍 1
s
Yep that's exactly right
👍 1
237 Views