From 912becb3a6abebb1d24df9aed7840a6af554d767 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 29 Nov 2021 21:10:09 +0200 Subject: [PATCH] Skip Django's `setUpTestData` mechanism in pytest-django tests --- pytest_django/fixtures.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 71e402a2..9cb73269 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -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)