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