Using class scoped pytest fixtures in testing

Hi there. I’d like to ask how django fellows are using pytest fixtures.

My team uses pytest-django. Some use traditional TestCase, some use pytest with fixture. Both tests are detected when we run pytest since we have set pytest.ini configuration.

My pain point is reusing pytest fixtures.

In django’s TestCase we use methods like setUpTestData , etc to bind data creation in class scope. But when we use pytest fixture, default scope of fixture is function. This is where problem comes.

You can technically change fixture scope to module, class or even session, but doing so is a bit clunky. Each test written in pytest style is guaranteed to be isolated between tests. It means database is also flushed between each test. When you set fixtures’ scope to more than function, they are recognised as the scope as I set, which is higher than function, but when second test is run data in database is not available since database is already flushed.

Changing all tests using TestCase seems too daunting. I wonder if there’s any elegant solution in this matter. So far not much progress.

Apology if it’s already addressed elsewhere. In that case I’d greatly appreciate if you redirect me to that discussion.