I Let Claude Write My Regression Tests, Then I Tried to Break Them
Every team I know has the same quiet problem. The regression suite is green, the CI badge looks proud, and yet bugs still slip into production in the exact flows the suite was supposed to protect.
The tests pass because they were written to pass. They check that a page loads, that a button exists, that nothing exploded. They rarely check the one thing that matters: did the user actually get the result they came for?
So I started using Claude Code to write regression tests with Playwright. The speed is real. What took an afternoon now takes twenty minutes. But speed created a new risk, and it is the subject of this post: an AI can write a confident, clean, completely useless test faster than any human.
The fix is not to slow down. The fix is to try to break every test the AI writes, before you merge it.
Why regression suites rot
Regression tests usually fail in three boring ways.
Weak assertions. The test visits the checkout page and asserts the heading is visible. The prices can be wrong, the total can be wrong, and the test still passes.
Happy path only. Nobody tests the expired card, the empty cart, or the session that times out at the worst moment.
Flaky selectors. The test depends on a class name or a long CSS chain. The next design cleanup breaks the test, not the feature, and the team learns to ignore red builds.
AI generated tests inherit all three habits, because they learn from the average test on the internet. Your job is to set a higher bar in the prompt, and then verify the result like a skeptic.
The rules I give Claude before it writes anything
I keep a short rules file in the repo and point Claude at it in every session. The rules are plain:
Test user outcomes, not page structure. Assert on totals, messages, and saved data.
Use role and label selectors first. CSS chains are a last resort.
One flow per test. If a test needs the word “and” in its name, split it.
No fixed sleeps. Wait for a visible state or a network response.
Every test must fail if the main action on the page is removed.
That last rule is the one that changes everything, and I will come back to it.
The workflow, step by step
Step 1: Pick one flow worth protecting.
Do not start with the whole suite. Start with the one flow that would hurt if it broke silently. For most products that is signup, checkout, or the main form that creates the core object in the app.
Step 2: Show Claude the real code.
Open the component, the route handler, and one existing good test from your repo. Claude writes much better tests when it can copy your project’s style instead of inventing its own.
Step 3: Ask for one test, not ten.
Generate a single test for the flow. Read every assertion yourself. This is the slow step, and it is supposed to be. You are setting the pattern the next twenty tests will follow.
Step 4: Run it, then read the failure output.
When the test fails, do not just ask Claude to fix it. Read the error first. Half the time the failure is a real bug in the app, and the AI has just earned its keep.
The prompt pattern that worked for me
Vague prompts produce vague tests. This is the shape of the prompt that consistently gives me tests worth reviewing:
Write one Playwright regression test for the checkout flow.
The test must assert the final order total and the
confirmation message, not just that the page loads.
Use getByRole and getByLabel selectors.
Do not use page.waitForTimeout anywhere.
After writing it, list the assertions and explain what
real user failure each one would catch.
That final instruction matters most. When Claude has to explain what each assertion catches, weak assertions expose themselves in its own summary.
The step most people skip: try to break the test
Here is the habit that turned this from a fun experiment into something I trust in CI. After a new test passes, I deliberately break the feature. I change the price calculation, remove the confirmation message, or return the wrong status from the API. Then I run the test again.
If the test still passes, it is not a regression test. It is decoration. I delete it or send it back with the failure it missed.
This idea is called mutation testing when tools do it automatically, and it is the core of the workflow I follow. The clearest end to end write up I have found is this guide on automating regression testing with Claude Code and Playwright, which walks through the same seven step flow, including the check that proves a test can actually catch a regression. It is worth reading before you scale this up to a full suite.
Manual breaking takes two minutes per test. It feels slow. It is still far cheaper than one production bug that your green suite waved through.
Three mistakes I made, so you do not have to
Letting the AI fix flaky tests by adding retries. A retry hides the disease. If a test is flaky, the cause is usually a race in the app or a bad wait in the test. Fix the cause.
Generating the whole suite in one session. By test number fifteen, I stopped reading carefully. That is exactly when the weakest test got merged. Small batches keep your review honest.
Testing what the mock returns. Claude loves mocking everything. A test that only talks to mocks proves the mock works. Keep mocks at the edge of the system, and let at least a few tests hit a real test environment.
A short checklist before you merge an AI written test
Does it assert a user visible outcome, not just page structure?
Would it fail if the main feature broke? Did you actually check?
Are the selectors based on roles and labels, not styling classes?
Is there any fixed sleep or timing guess in the code?
Can a new teammate understand the test name without opening the file?
If a test fails two or more of these, it is not ready, no matter how clean the code looks.
Final thoughts
Claude did not replace the thinking part of testing. It replaced the typing part. Deciding what is worth protecting, and proving the protection is real, is still the engineer’s job.
Used this way, the trade is excellent. You get a regression suite in days instead of weeks, and the mutation check keeps it honest. Start with one flow, one proven test, and scale only what survives being broken.
If you try this workflow, I would love to hear in the comments which flow you protected first, and whether your first AI written test survived the break test.
Top comments (0)