Guide: Pytest Integration

Set up Receiver.email as a Pytest fixture for async email testing. This guide covers installation, fixtures, and example tests.

Prerequisites

bash
pip install receiverdotemail pytest pytest-asyncio pytest-timeout

Create a Fixture

Add a client fixture and an inbox fixture in conftest.py. The client reads RECEIVER_API_KEY and finds the key's project by itself, and every test gets a fresh address:

conftest.py
import pytest
from receiverdotemail import ReceiverClient

@pytest.fixture
async def receiver():
    """Receiver.email client (reads RECEIVER_API_KEY)."""
    async with ReceiverClient() as client:
        yield client

@pytest.fixture
async def inbox(receiver):
    """A fresh address for every test."""
    return await receiver.inboxes.create(prefix="pytest")

Write Tests

test_emails.py
import pytest
from receiverdotemail import ReceiverTimeoutError

async def test_welcome_email(receiver, inbox):
    """Test that signup sends a welcome email."""
    # Trigger your app to send a welcome email
    await your_app.signup(email=inbox.address, password="test123")

    # Raises ReceiverTimeoutError, failing the test, if nothing arrives in 30 s
    email = await receiver.emails.wait_for(
        recipient=inbox.address,
        since=inbox.created_at,
        subject="welcome",  # case-insensitive; skips a confirmation email
        timeout=30,
    )

    assert len(email.links) > 0

async def test_password_reset(receiver, inbox):
    """Test password reset email contains a valid link."""
    await your_app.signup(email=inbox.address, password="test123")
    await your_app.request_password_reset(inbox.address)

    # The subject filter skips the sign-up emails
    email = await receiver.emails.wait_for(
        recipient=inbox.address,
        since=inbox.created_at,
        subject="reset",
        timeout=30,
    )

    assert any("/reset" in link for link in email.links)

async def test_otp_code(receiver, inbox):
    """Test OTP email contains a 6-digit code."""
    await your_app.send_otp(inbox.address)

    # Finds the code in the subject or body; code_length skips other numbers
    otp = await receiver.emails.wait_for_otp(
        recipient=inbox.address,
        since=inbox.created_at,
        code_length=6,
        timeout=30,
    )

    assert otp.code.isdigit() and len(otp.code) == 6

async def test_no_email_when_unsubscribed(receiver, inbox):
    """Test that an unsubscribed user gets nothing."""
    await your_app.send_newsletter(inbox.address, subscribed=False)

    with pytest.raises(ReceiverTimeoutError):
        await receiver.emails.wait_for(
            recipient=inbox.address, since=inbox.created_at, timeout=10
        )

Configure Pytest

Set up pytest.ini or pyproject.toml for async support and appropriate timeouts (timeout comes from pytest-timeout):

pytest.ini
[pytest]
asyncio_mode = auto
asyncio_default_fixture_loop_scope = function
timeout = 60

Run Tests

bash
# Set your API key
export RECEIVER_API_KEY="recv_live_..."

# Run tests
pytest tests/test_emails.py -v

# Run with coverage
pytest tests/test_emails.py --cov=your_app -v

Tips

  • Use asyncio_mode = auto in pytest config so you don't need to mark every test with @pytest.mark.asyncio.
  • Increase timeout: Email tests involve network I/O. Set pytest timeout to at least 60 seconds.
  • Unique addresses: inboxes.create() appends six random characters to the prefix, so parallel tests (pytest-xdist included) never share an address.
  • No None checks: wait_for() and wait_for_otp() never return None. When nothing arrives they raise ReceiverTimeoutError.
  • Environment variables: Keep the API key in RECEIVER_API_KEY, not in code. Setting RECEIVER_PROJECT_ID as well saves each client a project lookup.

Next Steps