channels icon indicating copy to clipboard operation
channels copied to clipboard

ChannelsLiveServerTestCase causes mail.outbox to always be empty

Open jackkinsella opened this issue 1 year ago • 4 comments

If I take a test that asserts against the contents of django.core.mail.outbox, it will pass with LiveServerTestCase but fail with ChannelsLiveServerTestCase

Setting EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" explicitly in my settings did not help.

jackkinsella avatar Nov 06 '25 16:11 jackkinsella

This appears to be caused by ChannelsLiveServerTestCase running the ASGI server in a separate process, meaning django.core.mail.backends.locmem.EmailBackend stores its outbox in a different memory space than the test process.

LiveServerTestCase works because the server and test run in the same process.

If that understanding is correct, I’d be happy to submit a documentation update clarifying that mail.outbox cannot be relied on with ChannelsLiveServerTestCase, and recommending alternative testing approaches (mocking, file-based backend, etc.).

CodeVishal-17 avatar Jan 04 '26 08:01 CodeVishal-17

@CodeVishal-17 This needs to be fixed rather than documented as a known issue I'd say.

carltongibson avatar Jan 04 '26 13:01 carltongibson

@carltongibson Thanks for the clarification — that makes sense.

Given that ChannelsLiveServerTestCase runs the ASGI server in a separate process, the current behavior of locmem.EmailBackend storing the outbox per-process seems to be the root cause.

I’m happy to explore a fix. One possible direction could be making the test email backend process-safe for Channels (e.g. via shared state or an alternative test backend), or adjusting ChannelsLiveServerTestCase to provide a compatible testing backend by default.

Before proceeding, do you have a preferred approach or prior discussion around this that I should be aware of?

CodeVishal-17 avatar Jan 04 '26 13:01 CodeVishal-17

Thanks for the detailed analysis that explanation about the separate ASGI process and locmem backend makes a lot of sense.

I’m following this issue and would be happy to help with investigation or test coverage if that would be useful. Please let me know if there’s a specific area where an extra set of hands would help.

AbdulHanan546 avatar Jan 27 '26 22:01 AbdulHanan546