Skip to content

Commit 9ef93bd

Browse files
committed
Skip Django's setUpTestData mechanism in pytest-django tests
1 parent d6ea40f commit 9ef93bd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pytest_django/fixtures.py

+26
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,32 @@ class PytestDjangoTestCase(test_case_class): # type: ignore[misc,valid-type]
199199
if _databases is not None:
200200
databases = _databases
201201

202+
# For non-transactional tests, skip executing `django.test.TestCase`'s
203+
# `setUpClass`/`tearDownClass`, only execute the super class ones.
204+
#
205+
# `TestCase`'s class setup manages the `setUpTestData`/class-level
206+
# transaction functionality. We don't use it; instead we (will) offer
207+
# our own alternatives. So it only adds overhead, and does some things
208+
# which conflict with our (planned) functionality, particularly, it
209+
# closes all database connections in `tearDownClass` which inhibits
210+
# wrapping tests in higher-scoped transactions.
211+
#
212+
# It's possible a new version of Django will add some unrelated
213+
# functionality to these methods, in which case skipping them completely
214+
# would not be desirable. Let's cross that bridge when we get there...
215+
if not transactional:
216+
@classmethod
217+
def setUpClass(cls) -> None:
218+
super(django.test.TestCase, cls).setUpClass()
219+
if (3, 2) <= VERSION < (4, 0):
220+
django.db.transaction.Atomic._ensure_durability = False
221+
222+
@classmethod
223+
def tearDownClass(cls) -> None:
224+
if (3, 2) <= VERSION < (4, 0):
225+
django.db.transaction.Atomic._ensure_durability = True
226+
super(django.test.TestCase, cls).tearDownClass()
227+
202228
PytestDjangoTestCase.setUpClass()
203229
if VERSION >= (4, 0):
204230
request.addfinalizer(PytestDjangoTestCase.doClassCleanups)

0 commit comments

Comments
 (0)