I use `isolationMode = InstancePerLeaf`. My class ...
# kotest
l
I use
isolationMode = InstancePerLeaf
. My class under test has a dependency, which is an
object
keeping some state (I don't want to mock it, as it's very simple). I was expecting the object to be torn down between tests, and constructed anew for every subsequent tests (so that the state isn't retained between tests). Turns out it isn't so, the object with its state is retained and influences subsequent tests.
s
That's because the object is created by the JVM not by Kotest.
You should avoid instance per leaf anyway. Do you have an example file, I can maybe point to an alternative
l
I use instance per leaf to isolate each test.
I want the behavior like in JUnit, that the test class is recreated from scratch for every test.
So that my tests don't influence each other
The project is proprietary unfortunately, basically it's a
FreeSpec
with some level of nesting (2-3 nested levels), but many tests are completely independent.
It's an Android project, I test a ViewModel using coroutines.
I create a new viewModel instance in every test and want to have fresh dependencies as well.
a
I',m using
InstancePerTest
with simple
StringSpec
which works for me. I concur - "test class is recreated from scratch for every test." is very important for lots of folks
l
Yes, I don't want to change the order of the tests and see some tests fail suddenly.
InstancePerTest
though seems weird to me. It runs the container tests (in specs that use them) separately as well, additionally to running them with the leaf test. So the container tests are effectively run multiple times. I'm not sure where this could be useful.
a
This whole isolation thing is getting reworked for next release - 6.0, so any short-term workaround might or might not port to 6.0
thank you color 1