Comparing email previews providers? Discover our new pricing options - chat to sales or book a demo to unlock your savings now

How to test OTPs automatically (without breaking the user experience)

One-time passcodes are a fundamental part of authentication, but how can you test them in CI/CD?

"How to test OTPs automatically", with the same email shown in light and dark modes (where the OTP is hard to read in dark mode)

One-time passcodes (OTPs) are now a fundamental part of modern authentication systems. From account verification to password resets, you’re hard pressed these days to find a company who doesn’t use OTPs as part of their flows.

This dependence on OTPs means that they have to work reliably every time, or your customers can’t login to their account, complete basic actions, or have control over their own use of your product. If a message isn’t delivered, if the code expires too quickly, or if the user can’t clearly see the code in their email or SMS message, the entire flow fails and your brand trust is impacted. Data shows that as many as 60% of abandoned logins in fintech apps alone are due to OTP delivery or validation faults.

With so much at stake, it’s surprising that OTP workflows are actually some of the least reliably tested parts of any application, with many teams depending manually checking inboxes and copying codes, or bypassing authentication testing entirely in staging environments.

What is OTP testing?

OTP testing is the process of validating that one-time password authentication flows behave correctly across an application’s communication channels.

A complete OTP test should verify that the OTP message is: triggered at the correct time, delivered, renders correctly, authentication is successful with the correct code, the code expires, and the code can’t be reused. OTP testing therefore includes a balance of functional testing, security validation, and communication verification.

Why OTP testing is critical for security

OTP-based authentication is often implemented to protect sensitive actions, such as account access, financial transactions, and password resets. In many cases, it forms part of a broader multi-factor authentication strategy designed to reduce the risk of unauthorized access.

If these systems are implemented incorrectly, they can introduce security vulnerabilities rather than prevent them. For example, a code that does not expire as intended, allows attackers more time to attempt unauthorized access. Similarly, if OTP codes can be reused, the system may allow repeated authentication with a previously accepted credential.

OTP testing also protects against operational failures. A system that fails to deliver authentication messages can lock legitimate users out of their accounts, or a poorly formatted email that hides the OTP code can cause confusion and increase support requests.

For organizations in regulated industries such as finance or healthcare, reliable authentication testing is also essential for demonstrating compliance with security and data protection requirements.

What actually needs to be tested in an OTP flow?

Many teams assume that OTP testing only means ensuring that access is permitted when the correct code is entered. In reality, a functioning OTP system depends on multiple interconnected components:

The OTP itself

The system needs to generate the OTP correctly, making it the correct length and format, and to associate it with the correct user session (storing it securely throughout).

Message delivery

The application needs to deliver the message, be it through email, SMS, or other channels. This delivery should happen immediately after the triggering event (for example account signup or login).

Message rendering

The OTP must appear clearly within the message itself, with formatting of the code being obvious to the user (and not obscured by layout issues or styling problems).

Authentication itself

The authentication endpoint must validate the OTP correctly, including confirming that the code matches the stored value and that it has not expired.

Security provisions

Finally, the system must enforce security rules such as expiration windows and single-use restrictions. If the code is reused or submitted after the allowed time period, authentication should fail.

Common OTP testing mistakes

OTP systems introduce complexities that are easy to overlook during testing, with several common mistakes appearing frequently across development teams:

  • Focusing on backend authentication logic only: Teams may write tests that validate the endpoint, without confirming that the OTP message itself was delivered correctly
  • Manual testing only: Developers often trigger a login flow and then copy the OTP code from a test inbox to complete authentication. While this approach may work during development, it does not scale and cannot be integrated into automated pipelines.
  • Ignoring rendering issues: Email clients render messages differently, and an OTP that appears clearly in one client may be hidden or poorly formatted in another.
  • Negative test scenarios: Teams sometimes neglect to test expiration and negative scenarios. OTP systems must reject expired codes and prevent reuse, but these cases are often left untested until problems appear in production.

How to automate OTP testing in CI/CD

Automation is the most reliable way to test OTP flows consistently. Instead of manually retrieving codes from emails or SMS messages, automated tests can capture and process these messages programmatically.

A typical automated OTP test follows several steps:

  1. The test triggers the authentication event. This might involve submitting a signup form, initiating a login request, or starting a password reset process.
  2. The application generates and sends the OTP message (in a test environment, this message is captured by a dedicated testing service rather than delivered to a real inbox or phone number)
  3. The test automation framework retrieves the message through an API. The OTP code is extracted from the message body and passed into the authentication verification step.
  4. The test confirms that authentication succeeds when the correct code is provided. Additional tests can validate that expired codes fail and that reused codes are rejected.

Integrating email previews into CI pipelines

Modern QA teams integrate both functional and visual OTP checks into their CI/CD pipelines. Using an email rendering testing API (such as Mailosaur), tests can automatically capture OTP emails, extract codes, and generate previews across Outlook, Gmail, iOS, Android, and dark mode.

Frameworks like Cypress and Playwright make it easy to validate OTP workflows end-to-end while also running cross‑client email rendering tests on every commit.

Why email rendering matters in OTP testing (email previews vs email testing)

Automating functional authentication testing is only part of the solution, especially considering that, OTP messages, at their core, are key customer communications that must be readable and usable.

Email rendering varies significantly between clients, and Gmail, Outlook, Apple Mail, and mobile devices all interpret HTML differently. There also needs to be consideration of settings as dark mode, for example, is another common source of problems as some email clients automatically invert colours, which can cause OTP codes to disappear or become difficult to read. Mobile devices introduce additional challenges. If the email layout is not responsive, the OTP code may appear too small or may be truncated.

The key takeaway here is that even a technically valid OTP is useless if the user cannot see it, and the way you can protect your customers from this issue is by previewing emails before you send them.

Testing OTP flows end-to-end

The most reliable approach to OTP testing, combining functional automation and visual verification, is through end-to-end testing.

This typically begins by triggering the authentication flow within the application, after which the test environment captures the resulting message and extracts the OTP automatically. The code is then used to complete the verification step, confirming that authentication succeeds when the correct OTP is provided, and additional tests confirm that expired codes and reused codes fail as expected.

Finally, preview testing confirms that the OTP message renders correctly across different email clients and devices. This holistic approach ensures that both the authentication logic and the user experience function correctly.

OTP testing best practices

Negative test scenarios should always be included in OTP testing; including always testing expiration rules explicitly, ensuring codes work one time only, and seeing what happens when an incorrect code is entered.

Authentication workflows should be tested across both email and SMS channels where applicable, ensuring that both communication paths behave consistently, and teams should validate the visual presentation of OTP messages using email previews across common clients and devices.

OTP testing as verification infrastructure

OTP messages need to be fundamentally secure, reliable, and user-friendly, and while these messages may be integral in authentication flows, they need to be rigorously tested as much as any other key messages.

If you want to automate OTP testing end‑to‑end, including functional verification, cross‑client rendering previews, and dark mode responsiveness, Mailosaur provides a complete testing platform designed for CI integration. Book your free demo now or start your 14-day free trial and see how it can fit into your test suite.