Skip to content

Commit 80cacd9

Browse files
committed
Skip Django's setUpTestData mechanism in pytest-django tests
1 parent 8f12646 commit 80cacd9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pytest_django/fixtures.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ class PytestDjangoTestCase(test_case_class): # type: ignore[misc,valid-type]
182182
if _databases is not None:
183183
databases = _databases
184184

185+
# For non-transactional tests, skip executing `django.test.TestCase`'s
186+
# `setUpClass`/`tearDownClass`, only execute the super class ones.
187+
#
188+
# `TestCase`'s class setup manages the `setUpTestData`/class-level
189+
# transaction functionality. We don't use it; instead we (will) offer
190+
# our own alternatives. So it only adds overhead, and does some things
191+
# which conflict with our (planned) functionality, particularly, it
192+
# closes all database connections in `tearDownClass` which inhibits
193+
# wrapping tests in higher-scoped transactions.
194+
#
195+
# It's possible a new version of Django will add some unrelated
196+
# functionality to these methods, in which case skipping them completely
197+
# would not be desirable. Let's cross that bridge when we get there...
198+
if not transactional:
199+
@classmethod
200+
def setUpClass(cls) -> None:
201+
super(django.test.TestCase, cls).setUpClass()
202+
if (3, 2) <= VERSION < (4, 0):
203+
django.db.transaction.Atomic._ensure_durability = False
204+
205+
@classmethod
206+
def tearDownClass(cls) -> None:
207+
if (3, 2) <= VERSION < (4, 0):
208+
django.db.transaction.Atomic._ensure_durability = True
209+
super(django.test.TestCase, cls).tearDownClass()
210+
185211
PytestDjangoTestCase.setUpClass()
186212
if VERSION >= (4, 0):
187213
request.addfinalizer(PytestDjangoTestCase.doClassCleanups)

0 commit comments

Comments
 (0)