Skip to content

Commit d071ff7

Browse files
committed
Replace addfinalizer uses with yield fixtures
1 parent 967618e commit d071ff7

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

docs/database.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,5 @@ Put this in ``conftest.py``::
528528
@pytest.fixture
529529
def db_access_without_rollback_and_truncate(request, django_db_setup, django_db_blocker):
530530
django_db_blocker.unblock()
531-
request.addfinalizer(django_db_blocker.restore)
531+
yield
532+
django_db_blocker.restore()

pytest_django/fixtures.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def django_db_setup(
108108
django_db_keepdb: bool,
109109
django_db_createdb: bool,
110110
django_db_modify_db_settings: None,
111-
) -> None:
111+
) -> Generator[None, None, None]:
112112
"""Top level fixture to ensure test databases are available"""
113113
from django.test.utils import setup_databases, teardown_databases
114114

@@ -127,7 +127,9 @@ def django_db_setup(
127127
**setup_databases_args
128128
)
129129

130-
def teardown_database() -> None:
130+
yield
131+
132+
if not django_db_keepdb:
131133
with django_db_blocker.unblock():
132134
try:
133135
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
@@ -138,19 +140,17 @@ def teardown_database() -> None:
138140
)
139141
)
140142

141-
if not django_db_keepdb:
142-
request.addfinalizer(teardown_database)
143-
144143

145144
@pytest.fixture()
146145
def _django_db_helper(
147146
request: pytest.FixtureRequest,
148147
django_db_setup: None,
149148
django_db_blocker,
150-
) -> None:
149+
) -> Generator[None, None, None]:
151150
from django import VERSION
152151

153152
if is_django_unittest(request):
153+
yield
154154
return
155155

156156
marker = request.node.get_closest_marker("django_db")
@@ -183,7 +183,6 @@ def _django_db_helper(
183183
)
184184

185185
django_db_blocker.unblock()
186-
request.addfinalizer(django_db_blocker.restore)
187186

188187
import django.db
189188
import django.test
@@ -233,13 +232,20 @@ def tearDownClass(cls) -> None:
233232
super(django.test.TestCase, cls).tearDownClass()
234233

235234
PytestDjangoTestCase.setUpClass()
236-
if VERSION >= (4, 0):
237-
request.addfinalizer(PytestDjangoTestCase.doClassCleanups)
238-
request.addfinalizer(PytestDjangoTestCase.tearDownClass)
239235

240236
test_case = PytestDjangoTestCase(methodName="__init__")
241237
test_case._pre_setup()
242-
request.addfinalizer(test_case._post_teardown)
238+
239+
yield
240+
241+
test_case._post_teardown()
242+
243+
PytestDjangoTestCase.tearDownClass()
244+
245+
if VERSION >= (4, 0):
246+
PytestDjangoTestCase.doClassCleanups()
247+
248+
django_db_blocker.restore()
243249

244250

245251
def validate_django_db(marker) -> _DjangoDb:
@@ -547,12 +553,12 @@ def live_server(request: pytest.FixtureRequest):
547553
) or "localhost"
548554

549555
server = live_server_helper.LiveServer(addr)
550-
request.addfinalizer(server.stop)
551-
return server
556+
yield server
557+
server.stop()
552558

553559

554560
@pytest.fixture(autouse=True, scope="function")
555-
def _live_server_helper(request: pytest.FixtureRequest) -> None:
561+
def _live_server_helper(request: pytest.FixtureRequest) -> Generator[None, None, None]:
556562
"""Helper to make live_server work, internal to pytest-django.
557563
558564
This helper will dynamically request the transactional_db fixture
@@ -568,13 +574,15 @@ def _live_server_helper(request: pytest.FixtureRequest) -> None:
568574
It will also override settings only for the duration of the test.
569575
"""
570576
if "live_server" not in request.fixturenames:
577+
yield
571578
return
572579

573580
request.getfixturevalue("transactional_db")
574581

575582
live_server = request.getfixturevalue("live_server")
576583
live_server._live_server_modified_settings.enable()
577-
request.addfinalizer(live_server._live_server_modified_settings.disable)
584+
yield
585+
live_server._live_server_modified_settings.disable()
578586

579587

580588
@contextmanager

pytest_django/plugin.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def get_order_number(test: pytest.Item) -> int:
463463

464464

465465
@pytest.fixture(autouse=True, scope="session")
466-
def django_test_environment(request: pytest.FixtureRequest) -> None:
466+
def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, None, None]:
467467
"""
468468
Ensure that Django is loaded and has its testing environment setup.
469469
@@ -487,7 +487,11 @@ def django_test_environment(request: pytest.FixtureRequest) -> None:
487487
debug = _get_boolean_value(debug_ini, "django_debug_mode", False)
488488

489489
setup_test_environment(debug=debug)
490-
request.addfinalizer(teardown_test_environment)
490+
yield
491+
teardown_test_environment()
492+
493+
else:
494+
yield
491495

492496

493497
@pytest.fixture(scope="session")
@@ -587,7 +591,7 @@ def django_mail_dnsname() -> str:
587591

588592

589593
@pytest.fixture(autouse=True, scope="function")
590-
def _django_set_urlconf(request: pytest.FixtureRequest) -> None:
594+
def _django_set_urlconf(request: pytest.FixtureRequest) -> Generator[None, None, None]:
591595
"""Apply the @pytest.mark.urls marker, internal to pytest-django."""
592596
marker = request.node.get_closest_marker("urls")
593597
if marker:
@@ -601,14 +605,14 @@ def _django_set_urlconf(request: pytest.FixtureRequest) -> None:
601605
clear_url_caches()
602606
set_urlconf(None)
603607

604-
def restore() -> None:
605-
django.conf.settings.ROOT_URLCONF = original_urlconf
606-
# Copy the pattern from
607-
# https://github.com/django/django/blob/main/django/test/signals.py#L152
608-
clear_url_caches()
609-
set_urlconf(None)
608+
yield
610609

611-
request.addfinalizer(restore)
610+
if marker:
611+
django.conf.settings.ROOT_URLCONF = original_urlconf
612+
# Copy the pattern from
613+
# https://github.com/django/django/blob/main/django/test/signals.py#L152
614+
clear_url_caches()
615+
set_urlconf(None)
612616

613617

614618
@pytest.fixture(autouse=True, scope="session")

tests/test_database.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Generator
2+
13
import pytest
24
from django.db import connection, transaction
35

@@ -188,9 +190,10 @@ def test_fixture_clean(self, all_dbs: None) -> None:
188190
assert Item.objects.count() == 0
189191

190192
@pytest.fixture
191-
def fin(self, request: pytest.FixtureRequest, all_dbs: None) -> None:
193+
def fin(self, request: pytest.FixtureRequest, all_dbs: None) -> Generator[None, None, None]:
192194
# This finalizer must be able to access the database
193-
request.addfinalizer(lambda: Item.objects.create(name="spam"))
195+
yield
196+
Item.objects.create(name="spam")
194197

195198
def test_fin(self, fin: None) -> None:
196199
# Check finalizer has db access (teardown will fail if not)

0 commit comments

Comments
 (0)