← Back to Blog

How to Test Email OTP Verification in CI

Automate email OTP testing in your CI pipeline without flaky timeouts or third-party integrations. Learn practical strategies for testing verification codes.

testingci-cdautomationotp
How to Test Email OTP Verification in CI

Testing email verification flows in continuous integration can be challenging. You need reliable OTP delivery, fast extraction, and no manual intervention. Traditional approaches often involve either mocking the email service entirely or waiting on real emails with flaky polling logic.

The Problem with Email Testing in CI

When building a service that sends verification codes via email, you face several challenges in automated testing:

  1. Real email providers are slow — SMTP delivery can take seconds or minutes
  2. Inbox polling is fragile — You need credentials, IMAP access, and complex parsing
  3. Mocking bypasses reality — Your tests pass, but production email fails
  4. Third-party integrations add dependencies — More services to configure and maintain

A Better Approach: Disposable Email with OTP Focus

Modern disposable email services designed for testing solve these problems by providing instant, OTP-first inboxes accessible via simple REST APIs.

Key Requirements for CI-Friendly OTP Testing

Your testing solution should provide:

  • Instant inbox creation without signup
  • REST API access for automation
  • OTP extraction built-in, not manual parsing
  • Fast delivery with real email receiving
  • Auto-expiry to prevent state leakage between tests

Implementation Example

Here's how you can test email OTP flows in CI using OTPBox:

# Create a disposable inbox
RESP=$(curl -s -X POST https://otpbox.app/api/inbox)
TOKEN=$(echo "$RESP" | jq -r '.token')
EMAIL=$(echo "$RESP" | jq -r '.email')

# Use the email in your application
curl -X POST https://your-app.com/api/register \
  -d "email=${EMAIL}"

# Poll for the OTP (often arrives within a few seconds)
sleep 2
OTP=$(curl -s "https://otpbox.app/api/inbox/${TOKEN}/latest" | jq -r '.otp')

# Verify the code
curl -X POST https://your-app.com/api/verify \
  -d "email=${EMAIL}&code=${OTP}"

The key advantage here is the /latest endpoint, which returns the most recent message with the OTP already extracted.

Integration with Test Frameworks

This pattern works well with any test framework. Here's a Python example using pytest:

import requests
import time

def create_test_inbox():
    response = requests.post('https://otpbox.app/api/inbox')
    data = response.json()
    token = data['token']
    email = data['email']
    return token, email

def get_latest_otp(token):
    response = requests.get(f'https://otpbox.app/api/inbox/{token}/latest')
    return response.json().get('otp')

def test_user_registration_flow():
    token, email = create_test_inbox()
    
    # Trigger registration
    requests.post('https://your-app.com/api/register', 
                  json={'email': email})
    
    # Wait briefly for email delivery
    time.sleep(2)
    
    # Get OTP
    otp = get_latest_otp(token)
    assert otp is not None
    
    # Verify
    response = requests.post('https://your-app.com/api/verify',
                           json={'email': email, 'code': otp})
    assert response.status_code == 200

Best Practices

When testing OTP verification in CI:

  1. Use unique inboxes per test — Avoid state pollution
  2. Keep short timeouts — If OTP doesn't arrive in 3-5 seconds, something is wrong
  3. Test failure paths too — Wrong codes, expired codes, rate limits
  4. Clean up is automatic — Disposable inboxes expire after use
  5. Document the flow — Make it clear that test emails use temporary addresses

Alternative Strategies

If you can't use external services, consider these approaches:

  • Local SMTP server like Mailpit or MailHog for development environments
  • Custom test transport that captures emails in memory during test runs
  • API-only verification with special test bypass codes (less realistic)

For comprehensive testing, we recommend a mix: unit tests with mocking, integration tests with local SMTP, and E2E tests with real disposable email services.

Related Articles

Resources

Testing email flows doesn't have to be complicated. With the right tools, you can verify OTP delivery end-to-end in seconds, keeping your CI pipeline fast and reliable.