FastAPI: test emails with one mailbox per run
Cuando una API envía un email de verificación, el test suele parecer sencillo: crear usuario, esperar el mensaje y abrir el enlace. El problema aparece cuando varios tests comparten el mismo buzón. Un mensaje viejo puede pasar por nuevo, un reintento puede leer el token de otra ejecución y el fallo termina siendo dificil de reproducir. En proyectos con FastAPI he encontrado una solución muy…
When testing APIs that send verification emails, problems arise when multiple tests share the same inbox, causing old messages to be mistaken for new ones or tokens to be incorrectly read. To solve this, a unique inbox and correlation identifier should be created for each test execution, along with a clear expiration date. A proposed solution involves creating a "fixture" that guarantees a unique identity, limited reading of messages, idempotent consumption, and expiration.
An example of a simple fixture for FastAPI is provided, which creates a unique inbox for each test and ensures that it is deleted after the test, even if an assertion fails. This approach helps prevent issues with test failures and makes it easier to debug and maintain tests.
Written by urgent.news from Dev.to's report — not a translation of it. Machine-written — may contain errors; check the original before relying on it.