@@ -425,8 +425,13 @@ def pytest_runtest_setup(item):
425
425
_disable_class_methods (item .cls )
426
426
427
427
428
+ @pytest .fixture (scope = "session" )
429
+ def _django_settings_is_configured ():
430
+ return django_settings_is_configured ()
431
+
432
+
428
433
@pytest .fixture (autouse = True , scope = "session" )
429
- def django_test_environment (request ):
434
+ def django_test_environment (_django_settings_is_configured ):
430
435
"""
431
436
Ensure that Django is loaded and has its testing environment setup.
432
437
@@ -437,18 +442,22 @@ def django_test_environment(request):
437
442
without duplicating a lot more of Django's test support code
438
443
we need to follow this model.
439
444
"""
440
- if django_settings_is_configured () :
445
+ if _django_settings_is_configured :
441
446
_setup_django ()
442
447
from django .conf import settings as dj_settings
443
448
from django .test .utils import setup_test_environment , teardown_test_environment
444
449
445
450
dj_settings .DEBUG = False
446
451
setup_test_environment ()
447
- request .addfinalizer (teardown_test_environment )
452
+
453
+ yield
454
+
455
+ if _django_settings_is_configured :
456
+ teardown_test_environment ()
448
457
449
458
450
459
@pytest .fixture (scope = "session" )
451
- def django_db_blocker ():
460
+ def django_db_blocker (_django_settings_is_configured ):
452
461
"""Wrapper around Django's database access.
453
462
454
463
This object can be used to re-enable database access. This fixture is used
@@ -461,10 +470,8 @@ def django_db_blocker():
461
470
This is an advanced feature that is meant to be used to implement database
462
471
fixtures.
463
472
"""
464
- if not django_settings_is_configured ():
465
- return None
466
-
467
- return _blocking_manager
473
+ if _django_settings_is_configured :
474
+ return _blocking_manager
468
475
469
476
470
477
@pytest .fixture (autouse = True )
@@ -486,9 +493,9 @@ def _django_db_marker(request):
486
493
487
494
488
495
@pytest .fixture (autouse = True , scope = "class" )
489
- def _django_setup_unittest (request , django_db_blocker ):
496
+ def _django_setup_unittest (request , django_db_blocker , _django_settings_is_configured ):
490
497
"""Setup a django unittest, internal to pytest-django."""
491
- if django_settings_is_configured () and is_django_unittest (request ):
498
+ if _django_settings_is_configured and is_django_unittest (request ):
492
499
request .getfixturevalue ("django_test_environment" )
493
500
request .getfixturevalue ("django_db_setup" )
494
501
@@ -528,23 +535,20 @@ def teardown():
528
535
529
536
530
537
@pytest .fixture (scope = "function" , autouse = True )
531
- def _dj_autoclear_mailbox ():
532
- if not django_settings_is_configured ():
533
- return
534
-
535
- from django .core import mail
538
+ def _dj_autoclear_mailbox (_django_settings_is_configured ):
539
+ if _django_settings_is_configured :
540
+ from django .core import mail
536
541
537
- del mail .outbox [:]
542
+ del mail .outbox [:]
538
543
539
544
540
545
@pytest .fixture (scope = "function" )
541
- def mailoutbox (monkeypatch , django_mail_patch_dns , _dj_autoclear_mailbox ):
542
- if not django_settings_is_configured ():
543
- return
546
+ def mailoutbox (monkeypatch , django_mail_patch_dns , _dj_autoclear_mailbox ,
547
+ _django_settings_is_configured ):
548
+ if _django_settings_is_configured :
549
+ from django .core import mail
544
550
545
- from django .core import mail
546
-
547
- return mail .outbox
551
+ return mail .outbox
548
552
549
553
550
554
@pytest .fixture (scope = "function" )
@@ -590,7 +594,7 @@ def restore():
590
594
591
595
592
596
@pytest .fixture (autouse = True , scope = "session" )
593
- def _fail_for_invalid_template_variable (request ):
597
+ def _fail_for_invalid_template_variable (_django_settings_is_configured ):
594
598
"""Fixture that fails for invalid variables in templates.
595
599
596
600
This fixture will fail each test that uses django template rendering
@@ -662,7 +666,7 @@ def __mod__(self, var):
662
666
663
667
if (
664
668
os .environ .get (INVALID_TEMPLATE_VARS_ENV , "false" ) == "true"
665
- and django_settings_is_configured ()
669
+ and _django_settings_is_configured
666
670
):
667
671
from django .conf import settings as dj_settings
668
672
@@ -675,12 +679,12 @@ def __mod__(self, var):
675
679
676
680
677
681
@pytest .fixture (autouse = True )
678
- def _template_string_if_invalid_marker (request ):
682
+ def _template_string_if_invalid_marker (request , _django_settings_is_configured ):
679
683
"""Apply the @pytest.mark.ignore_template_errors marker,
680
684
internal to pytest-django."""
681
685
marker = request .keywords .get ("ignore_template_errors" , None )
682
686
if os .environ .get (INVALID_TEMPLATE_VARS_ENV , "false" ) == "true" :
683
- if marker and django_settings_is_configured () :
687
+ if marker and _django_settings_is_configured :
684
688
from django .conf import settings as dj_settings
685
689
686
690
if dj_settings .TEMPLATES :
@@ -690,12 +694,11 @@ def _template_string_if_invalid_marker(request):
690
694
691
695
692
696
@pytest .fixture (autouse = True , scope = "function" )
693
- def _django_clear_site_cache ():
697
+ def _django_clear_site_cache (_django_settings_is_configured ):
694
698
"""Clears ``django.contrib.sites.models.SITE_CACHE`` to avoid
695
699
unexpected behavior with cached site objects.
696
700
"""
697
-
698
- if django_settings_is_configured ():
701
+ if _django_settings_is_configured :
699
702
from django .conf import settings as dj_settings
700
703
701
704
if "django.contrib.sites" in dj_settings .INSTALLED_APPS :
0 commit comments