Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,18 @@ def _dj_autoclear_mailbox() -> None:

@pytest.fixture
def mailoutbox(
request: pytest.FixtureRequest,
django_mail_patch_dns: None, # noqa: ARG001
_dj_autoclear_mailbox: None,
) -> list[django.core.mail.EmailMessage] | None:
"""A clean email outbox to which Django-generated emails are sent."""
skip_if_no_django()

# Django's TestCase._pre_setup() replaces mail.outbox. Run the database
# helper first so mailoutbox returns the replacement list.
if "_django_db_helper" in request.fixturenames:
request.getfixturevalue("_django_db_helper")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn you're right, i'm tempted to not do this and set the mail.outbox = [] myself here, to not instantiate the db.

@bluetech thoughts?


from django.core import mail

if hasattr(mail, "outbox"):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,23 @@ def test_mail_again(mailoutbox) -> None:
test_mail(mailoutbox)


def test_mailoutbox_with_db(mailoutbox, db: None) -> None: # noqa: ARG001

@kingbuzzman kingbuzzman Aug 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_mailoutbox_with_db(mailoutbox, db: None) -> None: # noqa: ARG001
def test_mailoutbox_without_db(mailoutbox) -> None: # noqa: ARG001

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have this test to make sure we don't regress yet again 😇

assert mailoutbox is mail.outbox


@pytest.fixture
def mail_sending_db_fixture(db: None) -> None: # noqa: ARG001
mail.send_mail("subject", "body", "from@example.com", ["to@example.com"])


def test_mailoutbox_with_db_dependent_fixture(
mailoutbox,
mail_sending_db_fixture: None, # noqa: ARG001
) -> None:
assert mailoutbox is mail.outbox
assert len(mailoutbox) == 1


def test_mail_message_uses_mocked_DNS_NAME(mailoutbox) -> None:
mail.send_mail("subject", "body", "from@example.com", ["to@example.com"])
m = mailoutbox[0]
Expand Down