I'm using pytest with Django (via pytest-django) and I'm wondering if it's possible to have a fixture of scope
class that creates some models in the database and then not have those models removed at the end of each test.
@pytest.fixture(scope='class') def expensive(): return MyModel.objects.create() @pytest.mark.django_db() class TestMyModel: def test_a(self, expensive): MyModel.objects.get() # All good def test_b(self, expensive): MyModel.objects.get() # raises MyModel.DoesNotExist
This is just a simplified example, in my actual code the fixture
expensive is actually doing something that takes some time (and I'm actually using a parametrize test but I imagine that won't make any difference). My desire is that the data created in the fixture
expensive is rolled back once out of the scope of the class so as to not interfere with other tests.
It appears as if what I'm trying to do might be possible using the fixture django_db_blocker however I was not able to get this to behave as I wanted.