I have a couple of fixtures that do some initialization that is rather expensive. Some of those fixtures can take parameters, altering their behaviour slightly.
Because these are so expensive, I wanted to do initialisation of them once per test class. However, it does not destroy and reinit the fixtures on the next permutation of parameters.
See this example: https://gist.github.com/vhdirk/3d7bd632c8433eaaa481555a149168c2
I would expect that StuffStub
would be a different instance when DBStub
is recreated for parameters 'foo' and 'bar'.
Did I misunderstand something? Is this a bug?
This is not a bug. There is no relation between the fixtures so one of them is not going to get called again just because the other one was due to having multiple params
.
In your case db
is called twice because db_factory
that it uses has 2 params
. The stuff
fixture on the other hand is called only once because stuff_factory
has only one item in params
.
You should get what you expect if stuff
would include db_factory
as well without actually using its output (db_factory
would not be called more than twice):
@pytest.fixture(scope="class")
def stuff(stuff_factory, db_factory):
return stuff_factory()