Skip to content

_dj_autoclear_mailbox ends quite often with AttributeError: module 'django.core.mail' has no attribute 'outbox' #993

Closed
@mcepl

Description

@mcepl

When running the test suite for whitenoise plenty of tests ends with the following error (using pytest-6.2.5, pytest-django-4.5.2):

[   31s] _______ ERROR at setup of test_last_modified_not_set_when_mtime_is_zero ________
[   31s]
[   31s]     @pytest.fixture(scope="function", autouse=True)
[   31s]     def _dj_autoclear_mailbox() -> None:
[   31s]         if not django_settings_is_configured():
[   31s]             return
[   31s]
[   31s]         from django.core import mail
[   31s]
[   31s] >       if mail.outbox:
[   31s] E       AttributeError: module 'django.core.mail' has no attribute 'outbox'
[   31s]

When reading on the mail.outbox I found that it doesn’t exist quite often, so this fixture has protection against it. When I apply this patch:

---
 pytest_django/plugin.py |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

--- a/pytest_django/plugin.py
+++ b/pytest_django/plugin.py
@@ -502,7 +502,8 @@ def _dj_autoclear_mailbox() -> None:

     from django.core import mail

-    del mail.outbox[:]
+    if hasattr(mail, "outbox"):
+        del mail.outbox[:]


 @pytest.fixture(scope="function")

Tests pass without a problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions