Home / Guides / Test 2FA codes
GDE.12Guide

How to test two-factor login (TOTP) without your phone

For developers and testers: how to sign in through two-factor authentication on staging and in automated tests, without passing a phone around.

Two-factor login is easy to switch on and annoying to test. Every time you sign in to a staging site, someone reaches for a phone, opens an authenticator app and types six digits before they expire. Automated tests can't do that at all. The good news is that the codes are not magic: they come from a shared secret and the current time, so anything that knows the secret can produce them.

This guide shows how to set up a dummy 2FA account for testing, read the secret from a 2FA QR code, check codes in your browser, generate them in automated tests, and fix the usual reasons codes fail. It applies to the standard kind of 2FA used by Google Authenticator, Microsoft Authenticator, 1Password, Authy and similar apps, known as TOTP.

How authenticator codes are made

TOTP stands for time-based one-time password, and it is defined in an open standard (RFC 6238). When you turn on 2FA, the site creates a random secret key and shows it to you as a QR code. Your authenticator app saves the secret. From then on, both sides run the same calculation:

  1. Take the current time and divide it into steps, usually 30 seconds long.
  2. Combine the step number with the secret using a keyed hash (HMAC, usually with SHA-1).
  3. Turn part of the result into a short number, usually 6 digits.

Because the phone and the server share the secret and agree on the time, they arrive at the same code without talking to each other. That is also why testing is possible without a phone: whoever holds the secret can calculate the code.

Set up a dummy 2FA account for testing

The cleanest approach is a test account whose secret is known and stored with your test settings. Testers and automated tests can then make valid codes whenever they need them, and nobody's personal phone is involved.

  • Use a separate test user on staging or your local machine, never a real customer or staff account.
  • Generate a fresh random secret for it, at least 160 bits (32 Base32 characters), so the test setup matches what real users get.
  • Store the secret like a password: in your test environment variables or secrets manager, not in a public repository.
  • Keep production secrets out of it. A test secret that leaks only exposes a test account. A real one exposes a real login.
Auth Key (TOTP) GeneratorCreate a random test secret and its QR code, then watch the live codes. Everything runs in your browser.
Open the tool

Read the secret from a 2FA QR code

A 2FA QR code holds a short line of text in a format called a key URI. It looks like this:

otpauth://totp/Example:test@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example&algorithm=SHA1&digits=6&period=30

Each part matters when you test:

PartWhat it meansUsual value
secretThe shared key, in Base32 (letters A to Z and digits 2 to 7)16 to 32 or more characters
issuerThe site name shown in the authenticator appYour app's name
algorithmThe hash used in the calculationSHA1
digitsLength of each code6
periodHow long each code lasts, in seconds30

To read a QR code, scan it with your phone's camera or any QR reader and copy the text it shows. The value after secret= is the key. When algorithm, digits or period are missing, apps assume SHA1, 6 digits and 30 seconds.

Only decode codes you own. Anyone with the secret can produce valid codes, so reading a QR code for a test account you set up is fine, but never decode or share the QR code for someone else's account.

Check codes in your browser

For manual testing, you don't need an app at all. Paste the test account's Base32 secret into the Auth Key Generator, set the same algorithm, digits and period as your app, and it shows the current code with a countdown. You can also paste a code from an authenticator app into its check box to confirm the two agree, which is a quick way to prove that a secret was saved correctly.

This is handy for a QA person working through a login flow on a laptop, or for a developer who wants to confirm their server produces the same code as a known-good implementation.

Generate codes in automated tests

End-to-end tests (Playwright, Cypress, Selenium and similar) can sign in through 2FA by calculating the code at the moment they need it. The pattern is the same in every language:

  1. Read the test account's secret from an environment variable.
  2. When the test reaches the 2FA screen, generate the current code with a TOTP library.
  3. Type it in and submit straight away.

Well-known libraries include pyotp for Python and otplib for Node.js, and most other languages have an equivalent. In Python, for example, it takes two lines:

import os, pyotp
code = pyotp.TOTP(os.environ["TEST_TOTP_SECRET"]).now()

One timing detail prevents flaky tests: if fewer than about three seconds remain in the current 30-second step, wait for the next step before generating the code. Otherwise the code can expire between being typed and being checked.

Why test codes fail

When a code that should work is rejected, check these in order:

  • Clock drift. TOTP depends on time, so a server or test machine whose clock is off by more than a step will disagree. Make sure both sync their time automatically. Many servers also accept the code from one step before or after, which absorbs small differences.
  • Mismatched settings. If your app uses SHA-256, 8 digits or a 60-second period but the generator or test library uses the defaults, every code will be wrong. Some authenticator apps ignore non-default settings entirely, which is one reason most sites stick with SHA1, 6 digits and 30 seconds.
  • A badly copied secret. Base32 secrets are often shown in groups with spaces, sometimes in lowercase. Remove the spaces and use uppercase. The digits 0, 1, 8 and 9 never appear in Base32, so a secret containing them was mistyped (often an O read as 0, or an I read as 1).
  • Codes reused too quickly. Many servers reject a code that has already been used, to stop replay attacks. If a test signs in twice within 30 seconds, the second attempt can fail even with the right code.
  • The wrong account. In an authenticator app full of entries, it is easy to read the code for a different site or environment. Give test accounts a clear issuer name, such as "Example (staging)".

Keep testing from weakening real security

Testing shortcuts have a habit of reaching production. A few habits keep 2FA honest:

  • Never add a "skip 2FA" switch that could be turned on in production. Use a known test secret instead, so the real code path is the one being tested.
  • Keep test secrets separate from real ones, and replace them if they appear somewhere they shouldn't, such as a shared screenshot.
  • Give real users backup codes when they enable 2FA, and test the backup-code login too.
  • Remember that anyone holding a secret can make codes. A real account's secret belongs in the user's authenticator app and your server, nowhere else.
Password & Key GeneratorMake strong random passwords and keys for your test accounts.
Open the tool

Need to hand test logins to a client or another developer? Send them as a self-destructing link rather than in an email. Our guide to sending passwords securely explains how.

Frequently asked questions

What is a dummy 2FA account?

It is a test account set up with a known, throwaway TOTP secret so developers and testers can produce valid codes on demand. The secret lives in your test configuration rather than on anyone's phone, and it must never be used for a real account.

Can I read the secret from a 2FA QR code?

Yes. A 2FA QR code is just text in the otpauth:// format, and the secret is the value after secret=. Any QR reader shows it. Treat it like a password: only decode QR codes for test accounts you control, never someone else's.

How do I generate TOTP codes in automated tests?

Store the test account's Base32 secret in your test environment and use a TOTP library, such as pyotp for Python or otplib for Node.js, to produce the current code when the test reaches the 2FA step. Use the same algorithm, digits and period as your app.

Why does my test code fail even though the secret is right?

The usual causes are clock drift on the server or test machine, a mismatch in algorithm, digits or period, a secret that was copied with spaces or lowercase letters, or a code that expired between being generated and submitted. Sync the clocks first, then compare the settings.

Is it safe to paste a TOTP secret into a website?

Only paste test secrets, and only into a tool that works offline in your browser. Our Auth Key Generator runs entirely in the browser and sends nothing to a server, but a real account's secret should stay in your authenticator app and your server, nowhere else.