Skip to content
TestLine Menu
TestLine
automation

How to test emails and SMS in automated tests

Where a test gets the email with an activation link or the code from an SMS, an example in Playwright with Mailpit, and a list of things to check in every message.

author
Artur Machowski, Senior QA Engineer
published
reading
6 min

Short answer

You check emails and SMS in automated tests through a tool that captures messages on the test environment and exposes them through an API: for mail, e.g. Mailpit (free, run next to the app), Mailtrap or MailSlurp; for SMS, a fake gateway or the provider’s test mode. The test triggers an action, fetches the latest message to a given address or number and checks the content, link or code. Leave real inboxes and phones for a few final tests, because they’re slow and flaky.

How it works

stepemailSMS
1. staging setupthe SMTP server points to Mailpit or Mailtrapthe SMS gateway is replaced by a fake or test mode
2. action in the testsign-up, password reset, orderlogin with a code, phone number confirmation
3. fetch the messagethe tool’s API, filtered by addressthe fake’s API, filtered by number
4. checkcontent, link, codecontent, code

Each test uses a unique address or number, e.g. [email protected], so parallel tests don’t read each other’s messages.

Example in Playwright with Mailpit

Password reset: the test requests a link, fetches the email through the Mailpit API and opens the link.

import { test, expect } from "@playwright/test";

const MAILPIT = process.env.MAILPIT_URL; // e.g. http://mailpit:8025

test("password reset through the emailed link", async ({ page, request }) => {
  const email = `qa-${Date.now()}@example.com`;
  // ... create an account for this address through the API

  await page.goto("/reset-password");
  await page.getByLabel("Email").fill(email);
  await page.getByRole("button", { name: "Send link" }).click();

  await expect.poll(async () => {
    const res = await request.get(`${MAILPIT}/api/v1/search?query=to:${email}`);
    return (await res.json()).messages.length;
  }).toBeGreaterThan(0);

  const search = await (await request.get(`${MAILPIT}/api/v1/search?query=to:${email}`)).json();
  const msg = await (await request.get(`${MAILPIT}/api/v1/message/${search.messages[0].ID}`)).json();
  const link = msg.Text.match(/https?:\/\/\S+reset\S+/)[0];

  await page.goto(link);
  await expect(page.getByLabel("New password")).toBeVisible();
});

expect.poll waits until the email arrives, instead of a fixed pause. Check the exact API paths in the documentation for the Mailpit version you use.

What to check in every message

  • recipient and subject;
  • data: amounts, order number, name, date, currency;
  • links: they point to the right environment and work, also when tapped on a phone;
  • codes: valid for a set time, invalid once used;
  • non-ASCII characters in the subject and body;
  • the plain-text version of the email, not just HTML;
  • no duplicates: one order, one email;
  • SMS: length, because non-GSM characters such as Polish letters or emoji shorten a single message from 160 to 70 characters.

How emails look in different clients (Outlook, Gmail, Apple Mail) is checked separately, once per template change, e.g. in the Mailtrap preview or an email preview tool.

Common mistakes

  • tests reading a real Gmail inbox: slow, with spam filters and 2FA;
  • a fixed “wait 10 seconds for the email” pause instead of waiting for a condition;
  • one shared address for all tests;
  • a staging environment that emails real customers because it has a copy of the production database.

How to set up staging with mail capture is covered in how to prepare a test environment. I discuss SMS codes for two-factor login in 2FA login in automated tests.

How I can help

As part of test automation I set up mail and SMS capture on the test environment and add E2E tests for sign-up, password reset and notifications. Describe your app in the contact form.

Frequently asked questions

How do you test emails in automated tests?

Route mail from the test environment to a tool that captures it and has an API, e.g. Mailpit, Mailtrap or MailSlurp. The test triggers an action, such as sign-up, then fetches the latest email to that address through the API and checks the content, link or code.

How do you test SMS in automated tests?

The simplest way is a fake SMS gateway on the test environment that stores messages and exposes them through an API. Some providers have a test mode or test numbers. Real SMS to testers' phones are sent only in a few final tests.

Can you use a real inbox, e.g. Gmail, in tests?

You can, but it's usually a source of flakiness: delays, spam filters, limits and logging in with 2FA. Capturing mail on staging is faster and repeatable.

What should you check in a transactional email?

The recipient, subject, correct data (amounts, order number, name), working links pointing to the right environment, non-ASCII characters, the plain-text version, the look in popular email clients, and that the email isn't sent twice.

How do you avoid sending emails to real customers from staging?

Configure staging so all mail goes to the capturing tool, not a real mail server. Also use addresses at example.com or your own test domain.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.