@@ -449,8 +449,13 @@ def get_order_number(test):
449449 items [:] = sorted (items , key = get_order_number )
450450
451451
452+ @pytest .fixture (scope = "session" )
453+ def _django_settings_is_configured ():
454+ return django_settings_is_configured ()
455+
456+
452457@pytest .fixture (autouse = True , scope = "session" )
453- def django_test_environment (request ):
458+ def django_test_environment (_django_settings_is_configured ):
454459 """
455460 Ensure that Django is loaded and has its testing environment setup.
456461
@@ -461,18 +466,22 @@ def django_test_environment(request):
461466 without duplicating a lot more of Django's test support code
462467 we need to follow this model.
463468 """
464- if django_settings_is_configured () :
469+ if _django_settings_is_configured :
465470 _setup_django ()
466471 from django .conf import settings as dj_settings
467472 from django .test .utils import setup_test_environment , teardown_test_environment
468473
469474 dj_settings .DEBUG = False
470475 setup_test_environment ()
471- request .addfinalizer (teardown_test_environment )
476+
477+ yield
478+
479+ if _django_settings_is_configured :
480+ teardown_test_environment ()
472481
473482
474483@pytest .fixture (scope = "session" )
475- def django_db_blocker ():
484+ def django_db_blocker (_django_settings_is_configured ):
476485 """Wrapper around Django's database access.
477486
478487 This object can be used to re-enable database access. This fixture is used
@@ -485,10 +494,8 @@ def django_db_blocker():
485494 This is an advanced feature that is meant to be used to implement database
486495 fixtures.
487496 """
488- if not django_settings_is_configured ():
489- return None
490-
491- return _blocking_manager
497+ if _django_settings_is_configured :
498+ return _blocking_manager
492499
493500
494501@pytest .fixture (autouse = True )
@@ -510,9 +517,9 @@ def _django_db_marker(request):
510517
511518
512519@pytest .fixture (autouse = True , scope = "class" )
513- def _django_setup_unittest (request , django_db_blocker ):
520+ def _django_setup_unittest (request , django_db_blocker , _django_settings_is_configured ):
514521 """Setup a django unittest, internal to pytest-django."""
515- if django_settings_is_configured () and is_django_unittest (request ):
522+ if _django_settings_is_configured and is_django_unittest (request ):
516523 request .getfixturevalue ("django_test_environment" )
517524 request .getfixturevalue ("django_db_setup" )
518525
@@ -552,23 +559,20 @@ def teardown():
552559
553560
554561@pytest .fixture (scope = "function" , autouse = True )
555- def _dj_autoclear_mailbox ():
556- if not django_settings_is_configured ():
557- return
558-
559- from django .core import mail
562+ def _dj_autoclear_mailbox (_django_settings_is_configured ):
563+ if _django_settings_is_configured :
564+ from django .core import mail
560565
561- del mail .outbox [:]
566+ del mail .outbox [:]
562567
563568
564569@pytest .fixture (scope = "function" )
565- def mailoutbox (monkeypatch , django_mail_patch_dns , _dj_autoclear_mailbox ):
566- if not django_settings_is_configured ():
567- return
570+ def mailoutbox (monkeypatch , django_mail_patch_dns , _dj_autoclear_mailbox ,
571+ _django_settings_is_configured ):
572+ if _django_settings_is_configured :
573+ from django .core import mail
568574
569- from django .core import mail
570-
571- return mail .outbox
575+ return mail .outbox
572576
573577
574578@pytest .fixture (scope = "function" )
@@ -614,7 +618,7 @@ def restore():
614618
615619
616620@pytest .fixture (autouse = True , scope = "session" )
617- def _fail_for_invalid_template_variable (request ):
621+ def _fail_for_invalid_template_variable (_django_settings_is_configured ):
618622 """Fixture that fails for invalid variables in templates.
619623
620624 This fixture will fail each test that uses django template rendering
@@ -686,7 +690,7 @@ def __mod__(self, var):
686690
687691 if (
688692 os .environ .get (INVALID_TEMPLATE_VARS_ENV , "false" ) == "true"
689- and django_settings_is_configured ()
693+ and _django_settings_is_configured
690694 ):
691695 from django .conf import settings as dj_settings
692696
@@ -699,12 +703,12 @@ def __mod__(self, var):
699703
700704
701705@pytest .fixture (autouse = True )
702- def _template_string_if_invalid_marker (request ):
706+ def _template_string_if_invalid_marker (request , _django_settings_is_configured ):
703707 """Apply the @pytest.mark.ignore_template_errors marker,
704708 internal to pytest-django."""
705709 marker = request .keywords .get ("ignore_template_errors" , None )
706710 if os .environ .get (INVALID_TEMPLATE_VARS_ENV , "false" ) == "true" :
707- if marker and django_settings_is_configured () :
711+ if marker and _django_settings_is_configured :
708712 from django .conf import settings as dj_settings
709713
710714 if dj_settings .TEMPLATES :
@@ -714,12 +718,11 @@ def _template_string_if_invalid_marker(request):
714718
715719
716720@pytest .fixture (autouse = True , scope = "function" )
717- def _django_clear_site_cache ():
721+ def _django_clear_site_cache (_django_settings_is_configured ):
718722 """Clears ``django.contrib.sites.models.SITE_CACHE`` to avoid
719723 unexpected behavior with cached site objects.
720724 """
721-
722- if django_settings_is_configured ():
725+ if _django_settings_is_configured :
723726 from django .conf import settings as dj_settings
724727
725728 if "django.contrib.sites" in dj_settings .INSTALLED_APPS :
0 commit comments