Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip Django's setUpTestData mechanism in pytest-django tests #974

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,32 @@ class PytestDjangoTestCase(test_case_class): # type: ignore[misc,valid-type]
if _databases is not None:
databases = _databases

# For non-transactional tests, skip executing `django.test.TestCase`'s
# `setUpClass`/`tearDownClass`, only execute the super class ones.
#
# `TestCase`'s class setup manages the `setUpTestData`/class-level
# transaction functionality. We don't use it; instead we (will) offer
# our own alternatives. So it only adds overhead, and does some things
# which conflict with our (planned) functionality, particularly, it
# closes all database connections in `tearDownClass` which inhibits
# wrapping tests in higher-scoped transactions.
#
# It's possible a new version of Django will add some unrelated
# functionality to these methods, in which case skipping them completely
# would not be desirable. Let's cross that bridge when we get there...
if not transactional:
@classmethod
def setUpClass(cls) -> None:
super(django.test.TestCase, cls).setUpClass()
if (3, 2) <= VERSION < (4, 1):
django.db.transaction.Atomic._ensure_durability = False

@classmethod
def tearDownClass(cls) -> None:
if (3, 2) <= VERSION < (4, 1):
django.db.transaction.Atomic._ensure_durability = True
super(django.test.TestCase, cls).tearDownClass()

PytestDjangoTestCase.setUpClass()
if VERSION >= (4, 0):
request.addfinalizer(PytestDjangoTestCase.doClassCleanups)
Expand Down