Wire up cls.setUpTestData for class level fixtures#972
Wire up cls.setUpTestData for class level fixtures#972paultiplady wants to merge 1 commit intopytest-dev:mainfrom
Conversation
Reimplement the Django TestCase's setUpTestData. Since we are already calling the setUpClass machinery from TestCase, it's a simple step to arrange for PytestDjangoTestCase to call the real test class's setUpTestData classmethod. This allows us to defer to the existing lifecycle hook machinery, and also use the Django 3.2 implementation for TestData which uses a descriptor to handle re-loading test instances between tests. (It does this by memoizing the pre-test instances, so this avoids us having to add a DB transaction or refresh_from_db() to reset the testData.)
|
WIP: So far I've just verified that unit tests run on Py 3.7, I want to get some directional feedback before spending time polishing this and getting everything green. |
|
I looked for solutions to the more general problem of "run session-scoped Django fixtures", but it turns out to be a bit challenging. The core problem is thusly: To write the most "pytestian" fixtures, I'd want to write a session-scoped fixture, something like @pytest.fixture(scope="session")
def item_1():
item_1 = Item.objects.create()
yield item_1However, there are a couple problems:
@blueyed suggested something like this: Which has the follow-on issue:
Also, 3. This does not let us use the state-of-the-art Maybe I'm overcomplicating this though -- I'm not familiar with the pytest machinery, so it's very likely there's an approach I didn't think of. |
Reimplement the Django TestCase's setUpTestData. Since we are
already calling the setUpClass machinery from TestCase, it's a
simple step to arrange for PytestDjangoTestCase to call the real
test class's setUpTestData classmethod.
This allows us to defer to the existing Django lifecycle hook machinery,
and also use the Django 3.2 implementation for TestData which
uses a descriptor to handle re-loading model instances between tests.
(It does this by memoizing the pre-test instances, so this avoids
us having to add a DB transaction or refresh_from_db() to reset the
testData.)
Partially fixes #514