https://kotlinlang.org logo
Title
d

dave08

05/24/2021, 3:45 PM
If I do
someArb.take(10).toList()
and then feed it to data test's
forAll
it only makes me one test 🤕?
s

sam

05/24/2021, 3:47 PM
there's 200 data tests now
which one are you using
d

dave08

05/24/2021, 3:47 PM
suspend fun <T : Any> ContainerContext.forAll(ts: List<T>, test: suspend (T) -> Unit)
s

sam

05/24/2021, 3:47 PM
so the new one from 4.5+
d

dave08

05/24/2021, 3:47 PM
4.6
s

sam

05/24/2021, 3:48 PM
what is .toList doing
is it making a list of t, or a list of a single element of list(t)
d

dave08

05/24/2021, 3:48 PM
I guess converting the sequence from arb's take to a list...?
s

sam

05/24/2021, 3:49 PM
here is a funky example
withData("p", "q") { a ->
      withData(listOf("r", "s")) { b ->
         withData(sequenceOf("x", "y")) { c ->
            a + b + c shouldHaveLength 3
            results.add(a + b + c)
         }
      }
   }
so it should all be working
d

dave08

05/24/2021, 3:51 PM
I don't have withData...? I think I might have some kind of caching problem...
But Intellij shows me:
s

sam

05/24/2021, 3:53 PM
its in
kotest-framework-datatest
but that should be coming in automatically
d

dave08

05/24/2021, 3:54 PM
s

sam

05/24/2021, 3:54 PM
yeah its not
damn that's an oversight
I'd add that module yourself
the DD testing is way better now
it does proper nesting and everything
d

dave08

05/24/2021, 4:00 PM
Well still the same problem... still only have one sample generated...
s

sam

05/24/2021, 4:00 PM
what if you do
withData(listOf(1,2) ){ a -> }
d

dave08

05/24/2021, 4:03 PM
Puzzling, I have to step out right now, but I tried that (using arb to generate to singles into a list) and still got one test... I'm using instancePerLeaf and
it
inside the withData block...
s

sam

05/24/2021, 4:04 PM
try pasting it in exactly what I put, just to rule out the data test framework
instance per leaf might be the issue
yes now I think about it, it definitely is
d

dave08

05/24/2021, 5:13 PM
But I need it so that my test setup can be reinitialized, is it supposed to work?
s

sam

05/24/2021, 5:33 PM
not for data tests
as they're dynamic so it can't find them again on the next pass
d

dave08

05/24/2021, 7:28 PM
Well at least it should fail with a proper error message?
s

sam

05/24/2021, 7:28 PM
agreed
d

dave08

05/24/2021, 7:29 PM
PerTest would still work with data tests?
If so, it could be suggested as a replacement in the error.
s

sam

05/24/2021, 7:30 PM
no that won't work either probably
just single instance
d

dave08

05/24/2021, 7:32 PM
Oh... Then it might be nice to have some kind of memoized construct like in spek? To reinitialize setup, since that's one of the reasons why i use InstancePerLeaf all the time
s

sam

05/24/2021, 7:33 PM
it wouldn't help with data tests
If you want to create a ticket, I will look into what can be done for 5.0
d

dave08

05/24/2021, 7:34 PM
Ok, I'll try to remember tomorrow when I get to work, thanks!
c

christophsturm

05/25/2021, 8:20 AM
if each context invocation uses the same random seed it would always create the same tests. that could be an easy fix.
d

dave08

05/25/2021, 1:21 PM
@sam Does
beforeTest
work with data tests?
s

sam

05/25/2021, 1:22 PM
Yes
d

dave08

05/25/2021, 1:22 PM
kotlin.UninitializedPropertyAccessException: lateinit property requestSequence has not been initialized
lateinit var requestSequence: Sequence<InitializeAccountRequest>

init {
    isolationMode = IsolationMode.SingleInstance

    describe("Initialization of account") {
        beforeTest {
            registrationState = setupRegistrationState(RegistrationState.arbValid().single()) {
                requestSequence = InitializeAccountRequest.arb(
                    odooDevice!!.id, oroAccount!!.id
                ).take(10)
            }
        }

        withData(requestSequence) { request ->
s

sam

05/25/2021, 1:24 PM
The nested before test might be causing a bug
It should work. I'm afk atm will check soon
d

dave08

05/25/2021, 1:25 PM
Yeah, when I take it out of describe it works...
s

sam

05/25/2021, 1:29 PM
Probably easy fix
d

dave08

05/25/2021, 1:32 PM
Can I use beforeContainer to setup before each data set?
s

sam

05/25/2021, 1:32 PM
Yes
d

dave08

05/25/2021, 2:02 PM
Oh boy! I suppose that withData grabs all the values from the sequence BEFORE it runs the test?
Since it seems that when I assert the state of the repos in the lateinit var already change... and they all fail.
So beforeContainer doesn't help at all in such a case...
And should withData contain `it`s or is it a test in itself @sam?
s

sam

05/25/2021, 3:29 PM
withData makes the tests for you
its basically like doing forEach with a test / it inside
d

dave08

05/25/2021, 3:30 PM
Oh... so asserting multiple named assertions isn't possible then... I usually put the Arrange and Act in a
context
and Assert in a bunch of `it`s to verify multiple assertions on the state coming out of the Act.
s

sam

05/25/2021, 3:31 PM
you can add further nests tests
withData(listOf(1,2)) { a ->

   context("set me up") { 
   }

    it("run me") {

    }
}
d

dave08

05/25/2021, 3:33 PM
So basically the same as:
it("..") {
   context("...") {
      it("...") {
         ...
      }
   }
}
and that works?
👍🏻 1
s

sam

05/25/2021, 3:34 PM
well not tried it but I don't see why it wouldn't
d

dave08

05/25/2021, 3:35 PM
only when surrounded by
context
? Also, maybe that's what caused my previous problems... https://kotlinlang.slack.com/archives/CT0G9SD7Z/p1621951379106600?thread_ts=1621871119.093600&amp;cid=CT0G9SD7Z
the beforeContainer might not be doing the initialisation properly when doing that...
And w/o nesting (no it inside withData), I get a funny <init> on a failing test...
Using #strikt for assertions that seem to work nicely with kotest so far...
s

sam

05/25/2021, 3:39 PM
yes if you wanted to use them
Kotest's assertions are richer
you can use both together
d

dave08

05/25/2021, 3:40 PM
But why the <init>?
s

sam

05/25/2021, 3:40 PM
you might have an error outside of the test, I don't knnow
and yes withData will materialize the full list
d

dave08

05/25/2021, 3:42 PM
Which makes it impossible to use lateinit vars in the class's context with SingleInstance... and still make sure they have the same state when asserting....
s

sam

05/25/2021, 3:43 PM
I think you can do what you want
d

dave08

05/25/2021, 3:43 PM
I'm a bit stuck with the setup part here... 🤕
s

sam

05/25/2021, 3:43 PM
Do you have a full example
d

dave08

05/25/2021, 3:44 PM
I'll try to DM part of it...