@@ -399,12 +399,36 @@ def teardown():
399399 request .addfinalizer (teardown )
400400
401401
402- @pytest .fixture (autouse = True , scope = 'function' )
403- def _django_clear_outbox (django_test_environment ):
404- """Clear the django outbox, internal to pytest-django."""
405- if django_settings_is_configured ():
406- from django .core import mail
407- del mail .outbox [:]
402+ class _DirectMailboxAccessProtector (list ):
403+
404+ def _raise_assertion (* args , ** kwargs ):
405+ raise AssertionError ('To access mail.outbox, use the mailoutbox fixture.' )
406+
407+ __len__ = __getitem__ = __nonzero__ = __bool__ = _raise_assertion
408+
409+
410+ @pytest .fixture (autouse = True )
411+ def _error_on_direct_mail_outbox_access (monkeypatch ):
412+ if not django_settings_is_configured ():
413+ return
414+
415+ from django .core import mail
416+
417+ outbox = _DirectMailboxAccessProtector ()
418+ monkeypatch .setattr (mail , 'outbox' , outbox )
419+ return outbox
420+
421+
422+ @pytest .fixture (scope = 'function' )
423+ def mailoutbox (monkeypatch , _error_on_direct_mail_outbox_access ):
424+ if not django_settings_is_configured ():
425+ return
426+
427+ from django .core import mail
428+
429+ outbox = list ()
430+ monkeypatch .setattr (mail , 'outbox' , outbox )
431+ return outbox
408432
409433
410434@pytest .fixture (autouse = True , scope = 'function' )
0 commit comments