Is there only supposed to be one `context` as root...
# kotest
d
Is there only supposed to be one
context
as root in a
ShouldSpec
when using
beforeEach
? It seems like when I put the
beforeEach
in the
init
block along with two
contexts
, it only gets run once...
s
beforeEach will run before each leaf test. context is not a leaf, so you would want beforeContainer or beforeAny
d
Thanks! And what's the difference between beforeEach and beforeTest then?
A leaf is the
should
?
Or the most inner context surrounding the should?
s
It's on the listeners doc page. I'm afk atm but can check later if you can't find it
d
I looked, but it wasn't too clear there...
I think the terminology needs to be worked on, defining the stages in respect to the various styles...
Ok, I reread the docs, I think its just something unexpected happening... sometimes the beforeSpecs/beforeTests get run all at once before the forAll tests and fail, and sometimes they get run before each leaf as expected by instancePerLeaf... I'm still looking into why...
s
Once you figure it out, please let me know.
d
It seems like there's a difference between using
override fun beforeXXX
or the lambda form
beforeXXX {
straight in the init block... the former sometimes runs the afterXXX before the container surrounding the should block on instancePerLeaf and the latter seems to work fine...
Could that be @sam?
s
There should be no difference because the latter just calls into the former
Can you make a small example that reproduces the issue, that would allow me to find and fix
d
It seems like a simpler test doesn't recreate the problem... 🤔 it might be linked to use of coroutines in various places and a few more factors involved in that test. If I manage to pinpoint it, I'll let you know.
I'll DM the original code just in case you can spot something though...
r
Stumbled upon this thread. To give some more context: A few versions ago only beforeTest existed. As this is executed before leaf tests AND containers (i.e describe/context block), there was no way to execute code only before leaf tests. Because of that, beforeEach and beforeContainer were added to support each individual use-case. To complete the circle beforeAny was also added, which does the same thing as the original beforeTest. So we now have • beforeEach - before leaf tests • beforeContainer - before containers • beforeAny - before both beforeTest doesn't do anything the above can't do, and in a perfect world beforeEach would be called beforeTest. In order to not introduce breaking changes it was left as is and beforeEach was chosen as a name