Staging Isn't Production: Wiring Disposable Numbers Into Your SMS Test Pipeline
Photo by Photo by Rob Wingate on Unsplash on Unsplash
Every team has one. A TEST_PHONE constant sitting in .env.local, pointing at somebody's actual cell. Maybe it's the founder's. Maybe it's the intern who left in March. Either way, your CI pipeline now depends on a human being who occasionally needs to text back a six-digit code.
That's not testing. That's a hostage situation with extra steps.
Phone verification deserves the same treatment as your database fixtures and your mock payment gateway: isolated, reproducible, and disposable. Here's how to get there.
The fake-number trap
First, the thing that doesn't work. Random number generators and "fake phone number" libraries feel clever for about a week. They emit something like +1 555 0100 or a syntactically valid 10-digit string, your validation layer accepts it, and your test goes green.
Then a real carrier rule shows up. Twilio rejects the prefix. Your SMS provider flags the destination as invalid. The number belongs to a rate-limited range, or it's on a Do Not Originate list, or it's a known test range your vendor has quietly blacklisted.
Now your tests pass in the sandbox and fail the moment you point them at anything resembling reality — which is precisely the failure mode testing exists to prevent. Fake numbers validate your regex. They don't validate your product.
Phone verification is a dependency, not a form field
The mental shift is simple: a phone number in your test suite is the same kind of object as a database connection or an S3 bucket. It has a lifecycle, it has a cost, it has an expiry, and it should be provisioned per run, not borrowed from a human.
That means three things:
- Provisioning — a source of real, working numbers owned by the test environment.
- Retrieval — a programmatic way to read the inbound code.
- Teardown — a number that stops mattering when the job ends.
Disposable numbers cover the first and third almost for free. The second is where the design work happens.
Reading codes without a human in the loop
If your SMS verification provider exposes an API, you can query the inbox directly. A polling helper usually looks something like this:
def wait_for_code(client, number_id, timeout=90, interval=3):
deadline = time.time() + timeout
while time.time() < deadline:
messages = client.get_messages(number_id)
for msg in messages:
match = re.search(r"\b(\d{4,8})\b", msg.body)
if match:
return match.group(1)
time.sleep(interval)
raise TimeoutError(f"no code arrived for {number_id}")
Two notes on that snippet, both learned the hard way.
Parse, don't assume. Codes are four to eight digits depending on the service. Some products send the code inside a sentence, some in a subject line, some via a shortlink that resolves to a page containing the code. Write a parser per vendor, not a global regex, and log the full message body when parsing fails.
Always time out. A test that waits forever on an SMS is worse than a failing test, because it burns your CI minutes and blocks the merge queue. Ninety seconds is generous. Most carriers deliver in under fifteen; anything past a minute means something is genuinely wrong.
Carrier filtering will find you
This is the part most teams discover in production, at 2 a.m., from a customer.
A2P messaging in the US is filtered aggressively. Messages get flagged for content, for sender reputation, for throughput, and for the shape of the destination number. A number that worked flawlessly on Monday can start silently dropping messages on Friday because the upstream aggregator changed a rule.
For test environments, that means:
- Don't assume instant delivery. Build retries with backoff into your helper, and assert on receipt rather than on latency.
- Test the failure path too. What does your UI do when the code never arrives? Does it offer a resend? Does it lock the user out after three attempts? Those flows deserve coverage as much as the happy path.
- Keep a small pool, rotate it. Reusing one number across hundreds of signups invites rate limiting on the destination side. Rotate and let the exhausted ones expire.
- Watch for per-number limits on the sender side. Twilio, Vonage, and friends all cap messages per number per second. If your test suite hammers a signup endpoint in parallel, you'll hit those caps before the carrier does.
Isolate test identities from production data
A disposable test number is only useful if you can tell it apart from a real user.
Tag every test account at creation — a is_test: true flag, a distinct email domain like @test.invalid, a recognizable name prefix. Then make sure those records can never leak into analytics, email sends, or billing. I've seen staging runs trigger real dunning emails to real addresses because a filter was applied in one place and not the other.
The cleanest setup keeps test traffic in its own database, its own API keys, and its own SMS provider subaccount. It's more wiring up front and it saves an entire quarter of cleanup later.
A short checklist before you ship
- No hardcoded personal digits anywhere in the repo, including
.env.example. - Test numbers provisioned per run or per suite, never shared across environments.
- Code retrieval is API-driven with a hard timeout and a logged failure body.
- Failure paths (no code, wrong code, expired code, locked account) all have tests.
- Rate limits respected — serialize when the sender caps you at one message per second.
- Test records flagged and excluded from every downstream pipeline.
Phone verification is infrastructure. Treat it like the rest of your stack and the 2 a.m. pages go away.