v0.145.2
 1"""Pytest fixtures for testing email — auto-registered via the ``pytest11`` entry point."""
 2
 3from __future__ import annotations
 4
 5from collections.abc import Generator
 6
 7import pytest
 8
 9from plain.email.backends.locmem import outbox
10from plain.runtime import settings
11
12_LOCMEM_BACKEND = "plain.email.backends.locmem.EmailBackend"
13
14
15@pytest.fixture
16def mailoutbox() -> Generator[list]:
17    """Email captured in memory for the duration of the test.
18
19    Routes ``EMAIL_BACKEND`` to the in-memory backend and yields its
20    ``outbox`` list (empty at the start of the test), restoring the
21    original backend afterward.
22    """
23    original = settings.EMAIL_BACKEND
24    settings.EMAIL_BACKEND = _LOCMEM_BACKEND
25    outbox.clear()
26    try:
27        yield outbox
28    finally:
29        settings.EMAIL_BACKEND = original
30        outbox.clear()