@@ -451,8 +451,13 @@ def get_order_number(test):
451
451
items [:] = sorted (items , key = get_order_number )
452
452
453
453
454
+ @pytest .fixture (scope = "session" )
455
+ def _django_settings_is_configured ():
456
+ return django_settings_is_configured ()
457
+
458
+
454
459
@pytest .fixture (autouse = True , scope = "session" )
455
- def django_test_environment (request ):
460
+ def django_test_environment (_django_settings_is_configured ):
456
461
"""
457
462
Ensure that Django is loaded and has its testing environment setup.
458
463
@@ -463,18 +468,22 @@ def django_test_environment(request):
463
468
without duplicating a lot more of Django's test support code
464
469
we need to follow this model.
465
470
"""
466
- if django_settings_is_configured () :
471
+ if _django_settings_is_configured :
467
472
_setup_django ()
468
473
from django .conf import settings as dj_settings
469
474
from django .test .utils import setup_test_environment , teardown_test_environment
470
475
471
476
dj_settings .DEBUG = False
472
477
setup_test_environment ()
473
- request .addfinalizer (teardown_test_environment )
478
+
479
+ yield
480
+
481
+ if _django_settings_is_configured :
482
+ teardown_test_environment ()
474
483
475
484
476
485
@pytest .fixture (scope = "session" )
477
- def django_db_blocker ():
486
+ def django_db_blocker (_django_settings_is_configured ):
478
487
"""Wrapper around Django's database access.
479
488
480
489
This object can be used to re-enable database access. This fixture is used
@@ -487,10 +496,8 @@ def django_db_blocker():
487
496
This is an advanced feature that is meant to be used to implement database
488
497
fixtures.
489
498
"""
490
- if not django_settings_is_configured ():
491
- return None
492
-
493
- return _blocking_manager
499
+ if _django_settings_is_configured :
500
+ return _blocking_manager
494
501
495
502
496
503
@pytest .fixture (autouse = True )
@@ -512,9 +519,9 @@ def _django_db_marker(request):
512
519
513
520
514
521
@pytest .fixture (autouse = True , scope = "class" )
515
- def _django_setup_unittest (request , django_db_blocker ):
522
+ def _django_setup_unittest (request , django_db_blocker , _django_settings_is_configured ):
516
523
"""Setup a django unittest, internal to pytest-django."""
517
- if not django_settings_is_configured () or not is_django_unittest (request ):
524
+ if not _django_settings_is_configured or not is_django_unittest (request ):
518
525
yield
519
526
return
520
527
@@ -553,23 +560,20 @@ def _cleaning_debug(self):
553
560
554
561
555
562
@pytest .fixture (scope = "function" , autouse = True )
556
- def _dj_autoclear_mailbox ():
557
- if not django_settings_is_configured ():
558
- return
559
-
560
- from django .core import mail
563
+ def _dj_autoclear_mailbox (_django_settings_is_configured ):
564
+ if _django_settings_is_configured :
565
+ from django .core import mail
561
566
562
- del mail .outbox [:]
567
+ del mail .outbox [:]
563
568
564
569
565
570
@pytest .fixture (scope = "function" )
566
- def mailoutbox (monkeypatch , django_mail_patch_dns , _dj_autoclear_mailbox ):
567
- if not django_settings_is_configured ():
568
- return
571
+ def mailoutbox (monkeypatch , django_mail_patch_dns , _dj_autoclear_mailbox ,
572
+ _django_settings_is_configured ):
573
+ if _django_settings_is_configured :
574
+ from django .core import mail
569
575
570
- from django .core import mail
571
-
572
- return mail .outbox
576
+ return mail .outbox
573
577
574
578
575
579
@pytest .fixture (scope = "function" )
@@ -615,7 +619,7 @@ def restore():
615
619
616
620
617
621
@pytest .fixture (autouse = True , scope = "session" )
618
- def _fail_for_invalid_template_variable (request ):
622
+ def _fail_for_invalid_template_variable (_django_settings_is_configured ):
619
623
"""Fixture that fails for invalid variables in templates.
620
624
621
625
This fixture will fail each test that uses django template rendering
@@ -687,7 +691,7 @@ def __mod__(self, var):
687
691
688
692
if (
689
693
os .environ .get (INVALID_TEMPLATE_VARS_ENV , "false" ) == "true"
690
- and django_settings_is_configured ()
694
+ and _django_settings_is_configured
691
695
):
692
696
from django .conf import settings as dj_settings
693
697
@@ -700,12 +704,12 @@ def __mod__(self, var):
700
704
701
705
702
706
@pytest .fixture (autouse = True )
703
- def _template_string_if_invalid_marker (request ):
707
+ def _template_string_if_invalid_marker (request , _django_settings_is_configured ):
704
708
"""Apply the @pytest.mark.ignore_template_errors marker,
705
709
internal to pytest-django."""
706
710
marker = request .keywords .get ("ignore_template_errors" , None )
707
711
if os .environ .get (INVALID_TEMPLATE_VARS_ENV , "false" ) == "true" :
708
- if marker and django_settings_is_configured () :
712
+ if marker and _django_settings_is_configured :
709
713
from django .conf import settings as dj_settings
710
714
711
715
if dj_settings .TEMPLATES :
@@ -715,12 +719,11 @@ def _template_string_if_invalid_marker(request):
715
719
716
720
717
721
@pytest .fixture (autouse = True , scope = "function" )
718
- def _django_clear_site_cache ():
722
+ def _django_clear_site_cache (_django_settings_is_configured ):
719
723
"""Clears ``django.contrib.sites.models.SITE_CACHE`` to avoid
720
724
unexpected behavior with cached site objects.
721
725
"""
722
-
723
- if django_settings_is_configured ():
726
+ if _django_settings_is_configured :
724
727
from django .conf import settings as dj_settings
725
728
726
729
if "django.contrib.sites" in dj_settings .INSTALLED_APPS :
0 commit comments