Skip to content

Python and pytest

Terminal window
pip install mailcatchr

Read the key and mailbox id from the environment, never from source.

conftest.py
import os
import time
import pytest
from mailcatchr import Client
@pytest.fixture(scope="session")
def mc():
return Client(api_key=os.environ["MAILCATCHR_API_KEY"])
@pytest.fixture(scope="session")
def mailbox_id():
return os.environ["MAILCATCHR_MAILBOX_ID"]
@pytest.fixture
def unique_address():
"""A fresh address per test so parallel workers never see each other's mail."""
def make(label: str) -> str:
return f"{label}-{int(time.time() * 1000)}@<mailbox-id>.mailcatchr.com"
return make
import re
def test_password_reset_email_contains_a_working_link(mc, mailbox_id, unique_address, app):
address = unique_address("reset")
app.request_password_reset(address)
summary = mc.messages.wait_for(mailbox_id, to=address, subject="Reset your password", timeout_seconds=30)
assert summary is not None, "the reset email should arrive within 30 s"
message = mc.messages.get(mailbox_id, summary.id)
link = re.search(r"https://\S+/reset\?token=\S+", message.text_body or "").group(0)
assert app.get(link).status_code == 200

wait_for returns None on timeout rather than raising, so the assertion message can say what was expected.

The same client works inside a browser test. Fetch a two-factor code mid-login:

def test_login_with_totp(page, mc):
page.goto("/login")
page.fill('input[name="email"]', "2fa-user@<mailbox-id>.mailcatchr.com")
page.fill('input[name="password"]', os.environ["TEST_USER_PASSWORD"])
page.click('button[type="submit"]')
page.fill('input[name="otp"]', mc.totps.code(os.environ["MAILCATCHR_TOTP_ID"]).code)
page.click('button[type="submit"]')
expect(page).to_have_url(re.compile(r"/dashboard"))

pytest-xdist workers share one mailbox safely as long as every test filters on its own recipient address. See GitHub Actions for isolating re-runs.