@@ -462,7 +462,7 @@ def django_test_environment(request):
462
462
463
463
464
464
@pytest .fixture (scope = "session" )
465
- def django_db_blocker ():
465
+ def _django_db_blocker ():
466
466
"""Wrapper around Django's database access.
467
467
468
468
This object can be used to re-enable database access. This fixture is used
@@ -481,6 +481,28 @@ def django_db_blocker():
481
481
return _blocking_manager
482
482
483
483
484
+ @pytest .fixture (scope = "session" )
485
+ def django_db_blocker (django_db_setup , _django_db_blocker ):
486
+ """Wrapper around _django_db_blocker to serve as convenience reference.
487
+
488
+ The ``_django_db_blocker`` fixture must be available for the ``django_db_setup``
489
+ fixture, so ``django_db_setup`` must request the ``_django_db_blocker`` fixture. But
490
+ in order for ``_django_db_blocker`` to be used, ``django_db_setup`` must also have
491
+ been executed, suggesting that ``_django_db_blocker`` should request
492
+ ``django_db_setup``, especially since it is possible for ``_django_db_blocker`` to
493
+ be needed when ``django_db_setup`` wouldn't normally have been run (e.g. if a test
494
+ isn't marked with ``pytest.mark.django_db``).
495
+
496
+ This would normally cause a catch-22, but to circumvent this, the
497
+ `_django_db_blocker`` fixture is used behind the scenes, while ``django_db_blocker``
498
+ serves as the fixture used by everything that would normally need the blocker (aside
499
+ from ``django_db_setup``). This fixture helps coordinate between both
500
+ ``django_db_setup`` and ``_django_db_blocker``, so that whenever
501
+ ``django_db_blocker`` gets used, it ensures ``django_db_setup`` is run first.
502
+ """
503
+ return _django_db_blocker
504
+
505
+
484
506
@pytest .fixture (autouse = True )
485
507
def _django_db_marker (request ):
486
508
"""Implement the django_db marker, internal to pytest-django.
@@ -500,7 +522,7 @@ def _django_db_marker(request):
500
522
501
523
502
524
@pytest .fixture (autouse = True , scope = "class" )
503
- def _django_setup_unittest (request , django_db_blocker ):
525
+ def _django_setup_unittest (request , _django_db_blocker ):
504
526
"""Setup a django unittest, internal to pytest-django."""
505
527
if not django_settings_is_configured () or not is_django_unittest (request ):
506
528
yield
@@ -525,7 +547,7 @@ def non_debugging_runtest(self):
525
547
526
548
cls = request .node .cls
527
549
528
- with django_db_blocker .unblock ():
550
+ with _django_db_blocker .unblock ():
529
551
if _handle_unittest_methods :
530
552
_restore_class_methods (cls )
531
553
cls .setUpClass ()
@@ -736,7 +758,7 @@ def __exit__(self, exc_type, exc_value, traceback):
736
758
class _DatabaseBlocker (object ):
737
759
"""Manager for django.db.backends.base.base.BaseDatabaseWrapper.
738
760
739
- This is the object returned by django_db_blocker.
761
+ This is the object returned by _django_db_blocker and django_db_blocker.
740
762
"""
741
763
742
764
def __init__ (self ):
0 commit comments