https://kotlinlang.org logo
Title
d

dave08

12/06/2020, 2:55 PM
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

sam

12/07/2020, 7:24 PM
beforeEach will run before each leaf test. context is not a leaf, so you would want beforeContainer or beforeAny
d

dave08

12/07/2020, 7:46 PM
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

sam

12/07/2020, 7:50 PM
It's on the listeners doc page. I'm afk atm but can check later if you can't find it
d

dave08

12/07/2020, 8:12 PM
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

sam

12/08/2020, 7:05 PM
Once you figure it out, please let me know.
d

dave08

12/09/2020, 2:47 PM
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

sam

12/09/2020, 2:48 PM
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

dave08

12/09/2020, 4:21 PM
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

reevn

02/13/2021, 4:37 PM
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