I have a fixture that initializes the database, and the process is quite expensive (create tables, insert some data, etc). The fixture is function-scoped, since normally tests mutate the state somehow, and it's not safe to share.
However, now I have a case where I have a bunch of read-only queries that I want to test. They can share the same state and that way execute faster. However, because the fixture is function-scoped, it seems Pytest provides no good way of doing it.
Approaches I tried:
1. pytest.mark.parametrize
each case executes the fixture again so no good
2. make the fixture module-scoped
this is maybe possible, but I'm using pytest-asyncio that defines the event loop fixture (which is used by virtually all of my fixtures) as function-scoped. The devs say it's not safe to make it module or session scoped.
Additional, if I go with this approach, I need to be able to switch between function-scoped or module-scoped fixtures somehow, so I only make it module-scoped for this particular group of tests.
3. write a single test with a for loop going through all the cases
that's what I have now, but I don't believe it's good
a. pytest treats it all as a single test, so if just one case fails, I have no idea if it's one or thirty
b. there's no easy way of running a single case; I usually just comment out all the stuff that I don't want to run
what would be amazing
Either of:
- painless way of making the fixtures module-scoped (or something), so I can split the test cases into separate tests
- keep the fixtures as they are, but make Pytest recognize the test cases in the loop as separate tests.