1"""
2Email test lifecycle, registered under the `plain.testing` entry point.
3
4Routes EMAIL_BACKEND to the in-memory backend for the whole run — tests
5never send real email — and clears the outbox before each test.
6"""
7
8from collections.abc import Generator
9from contextlib import ExitStack, contextmanager
10
11from plain.testing import CollectedTest, TestLifecycle, override_settings
12
13from ..backends.locmem import outbox
14
15_LOCMEM_BACKEND = "plain.email.backends.locmem.EmailBackend"
16
17
18class EmailTestLifecycle(TestLifecycle):
19 required_package = "plain.email"
20
21 def __init__(self) -> None:
22 # Holds the setting overridden from setup to teardown.
23 self._in_memory_backend = ExitStack()
24
25 def setup_worker(self) -> None:
26 self._in_memory_backend.enter_context(
27 override_settings(EMAIL_BACKEND=_LOCMEM_BACKEND)
28 )
29
30 def teardown_worker(self) -> None:
31 self._in_memory_backend.close()
32
33 @contextmanager
34 def around_test(self, test: CollectedTest) -> Generator[None]:
35 outbox.clear()
36 yield