pytest + plop.email

Test email functionality in Python applications. Works with Django, FastAPI, Flask, and any Python framework.

pytest Documentation

Features

Fixtures

Create pytest fixtures for email testing setup.

Parametrize

Test multiple email scenarios with @pytest.mark.parametrize.

Async Support

Use pytest-asyncio for async email fetching.

Framework Agnostic

Works with Django, FastAPI, Flask, and more.

Setup

# Install pytest and requests
pip install pytest requests python-dotenv

# Create conftest.py with fixtures
# tests/conftest.py
import pytest
import requests
import os
import time

@pytest.fixture
def plop_client():
    class PlopClient:
        def __init__(self):
            self.base_url = "https://api.plop.email/v1"
            self.api_key = os.environ["PLOP_API_KEY"]

        def get_latest(self, to: str):
            response = requests.get(
                f"{self.base_url}/messages/latest",
                params={"to": to},
                headers={"Authorization": f"Bearer {self.api_key}"},
            )
            return response.json()

    return PlopClient()

Basic Email Test

Test email sending in a Python application.

Basic Email Testtypescript
import pytest
import time
from myapp.email import send_welcome_email

def test_welcome_email(plop_client):
    test_email = f"pytest+{int(time.time())}@in.plop.email"

    # Send email from your application
    send_welcome_email(to=test_email, name="Test User")

    # Wait for delivery
    time.sleep(2)

    # Fetch and verify
    email = plop_client.get_latest(test_email)

    assert "Welcome" in email["subject"]
    assert "Test User" in email["htmlContent"]
    assert "Get Started" in email["htmlContent"]

Parametrized Email Tests

Test multiple email templates with parametrize.

Parametrized Email Teststypescript
import pytest
import time

@pytest.fixture
def unique_email():
    return f"pytest+{int(time.time() * 1000)}@in.plop.email"

@pytest.mark.parametrize("template,expected_subject,expected_content", [
    ("welcome", "Welcome", ["Get Started", "Dashboard"]),
    ("password_reset", "Reset Your Password", ["Reset Link", "expires in"]),
    ("invoice", "Your Invoice", ["Amount Due", "Pay Now"]),
])
def test_email_templates(
    plop_client,
    unique_email,
    template,
    expected_subject,
    expected_content
):
    # Trigger email
    send_email(template=template, to=unique_email)
    time.sleep(2)

    # Verify
    email = plop_client.get_latest(unique_email)

    assert expected_subject in email["subject"]
    for content in expected_content:
        assert content in email["htmlContent"], f"Missing: {content}"

Tips

  • Use pytest fixtures for DRY test setup
  • Add @pytest.mark.integration for email tests
  • Use freezegun for testing time-sensitive emails
  • Consider pytest-asyncio for async frameworks like FastAPI

Related Integrations

Start testing with pytest

Add email verification to your pytest tests in minutes.