Skip to main content

Tests That Fail for the Right Reason

Why This Matters

A green test proves something only if it could have been red. Two kinds of test look green and prove nothing:

  • The flaky test. Green four times, red the fifth, because it doesn’t wait for the right thing. Nobody trusts it, so everybody ignores it, including the day it is right.
  • The empty test. Its assertion can’t fail: it compares a value with itself, checks something that is always there, or is never reached. It stays green while the feature is broken.

AI-generated tests have both problems more often than hand-written ones, because the AI writes a test that passes now and moves on. verify_test_stability checks a test for both, without any AI involved: it only runs the test. This is guardrail 6 from lesson 4.

What you will learn

  • What verify_test_stability does: stability runs and assertion mutants
  • How to read its result, passed and not passed
  • Why a test that stays green with a wrong expected value checks nothing
  • How verification is tied to one version of the test
  • The limits: what the check can and cannot prove

Two checks in one tool

verify_test_stability takes a test file from the manifest and runs:

  1. Stability. The test N times in a row. One red run means the test is not accepted: if it was red the first time it is failing; if it was green first and red later, it is flaky.
  2. Fails for the right reason. Up to three mutants: copies of the test with one expected value changed. Each copy must go red. A copy that stays green shows an assertion that checks nothing.

N comes from the profile: 3 under guided, 5 under strict (lesson 16). Under explore both checks are off by default, but you can still ask for them: “verify this test with 5 runs and the fail check.”

The tool never uses an LLM: it runs the test with the manifest’s run command, the same way CI does. The result is the same every time.

What a mutant is

A mutant changes exactly one expected value in one check, and nothing else: a string gets the suffix __mutated__, a number changes. Locators, timeouts and code are never touched, so the copy still runs; if it goes red, it is because the check noticed.

For test 1.1 from lesson 7:

assert "Search Results (10 products)" in " ".join(heading.split())
assert "0 items" in shop.cart_text() and "$0.00" in shop.cart_text()
shop.wait_for_cart(1, "$89.99")

the mutants are:

assert "Search Results (10 products)__mutated__" in " ".join(heading.split())
assert "0 items" in shop.cart_text() and "$0.00__mutated__" in shop.cart_text()
shop.wait_for_cart(1, "$89.99__mutated__")

The first copy expects a heading the page doesn’t have: it must go red. The third copy waits for a cart total that never appears: the wait times out and the test goes red. If any copy stays green, that check doesn’t do what it seems to.

The copies are written next to the test (test_add_from_grid__mutant1.py, so the runner picks them up), run, and deleted.

Reading the result

A test that passes both checks returns something like this (shortened):

{
  "message": "Verified: the test is stable and fails for the right reason",
  "passed": true,
  "runs": [ { "run": 1, "exitCode": 0 }, { "run": 2, "exitCode": 0 }, { "run": 3, "exitCode": 0 } ],
  "failCheck": {
    "mutants": [
      { "line": 6, "changed": "assert \"Search Results (10 products)__mutated__\" in ...", "caught": true },
      { "line": 8, "changed": "assert \"0 items\" in shop.cart_text() and \"$0.00__mutated__\" in ...", "caught": true },
      { "line": 12, "changed": "shop.wait_for_cart(1, \"$89.99__mutated__\")", "caught": true }
    ],
    "caught": 3
  },
  "next": "Done: the test counts as verified until the file changes."
}

And the reasons a test is not verified, each with the tool’s own next step:

ReasonMeansNext step the tool gives
the test failsRed on the first runDebug it with healer_debug_test. If the app is broken, report it instead of healing the test.
flaky: red on run 4 after 3 greenIt doesn’t wait for the right thingFind the condition the test does not wait for (not a sleep or a longer timeout), fix it, verify again.
1 of 3 changed expected values stayed green: those assertions check nothingAn empty checkStrengthen the assertions that stayed green: compare the exact text or value the user sees.
no assertion with an expected value to changeThe test checks nothing the user seesAdd an assertion on what the user sees.

What an empty check looks like

Three patterns that pass a test and stay green under a mutant:

# 1. Checks that *something* is there, not *what*
assert shop.find("cart-button").text            # any text passes

# 2. Compares the page with itself
total = shop.find("order-total").text
assert total == shop.find("order-total").text   # always true

# 3. An assertion that is never reached
if shop.driver.find_elements(*tid("payment-success")):
    assert shop.visible("payment-success").text == "Payment Successful!"

The first two have no expected value to mutate. The third has one, but its mutant stays green whenever the success message is missing, which is exactly when it matters. In each case the fix is the same: compare with the concrete value from the plan’s expect: line.

Verified means this version

The result is stored with the test in .test-manifest.json, together with a hash of the file’s content. For example:

{
  "file": ".../tests/cart/test_add_from_grid.py",
  "specFile": "specs/shopping-cart-and-checkout.md",
  "verification": {
    "codeHash": "3f9a1c0e7b2d4a65",
    "passed": true,
    "runs": 3,
    "greenRuns": 3,
    "failCheck": { "mutants": 3, "caught": 3 },
    "profile": "guided",
    "at": "2026-12-02T10:31:12.000Z"
  }
}

Change one character in the test (by hand, by the healer, by regenerating) and the hash no longer matches: the test is unverified again. Under guided and strict, the generator and healer remind the agent after every write:

Not verified yet. The "guided" profile needs 3 green runs in a row and the fails-for-the-right-reason check:
call verify_test_stability with this file before reporting the test as done.

The CI gate (lesson 18) checks this hash, so an unverified test can’t slip into the main branch.

The limits

Be clear about what this proves and what not:

  • Three mutants per test at most, one per check line from the top. A test with ten assertions has its first three proven, not all ten.
  • Java and C# are skipped. A mutant copy would duplicate the test class and break the build. Use the language’s own mutation tool there: PIT for Java, Stryker.NET for C#. The result says so (skipped).
  • Stability is not proof of no flakiness. Five green runs make flakiness unlikely, not impossible: a test that fails once in fifty will usually pass five runs. Under load in CI, flaky tests still show up; the run history (lesson 14) helps you spot them.
  • A mutant proves the check can fail, not that it checks the right thing. If the plan says “$60.90” and the shop shows a wrong “$60.90”, the test is green and right about the wrong value. That is the review’s job (lesson 8).

Exercise

  1. Verify one of your generated tests: “Verify tests/cart/test_add_from_grid.py with verify_test_stability.” How many runs, how many mutants?
  2. Make the test weaker: replace shop.wait_for_cart(1, "$89.99") with assert shop.cart_text(). Verify again. What does the result say?
  3. Restore the test, but add time.sleep(1) after the click and ask the agent to save it with generator_write_test. What happens? (Lesson 13.)
Show suggested answer
  1. Under guided: 3 runs and 3 mutants (the heading, the empty cart, the cart after adding). All caught: verified.
  2. The weak assertion has no expected value to change. Depending on the other lines, the result says either that a changed expected value stayed green, or (if no checks are left) no assertion with an expected value to change: … add an assertion on what the user sees. Either way: Not verified. Note that the plain test run is still green: only the verification shows the test got weaker.
  3. generator_write_test refuses it: no-sleep is an error under every profile. The test is never written, so there is nothing to verify.

Exam-style questions

Quiz

1. A mutant of a test, with one expected value changed, stays green. What does that mean?

  • A. The test is flaky
  • B. The application is broken
  • C. That assertion checks nothing: it passes whatever the value is
  • D. The mutant was not run
Show answer
C.
Quiz

2. A test was verified yesterday. Today a developer changes a comment in it. Is it still verified?

  • A. Yes, comments don’t matter
  • B. No: verification is tied to the file’s hash, and any change makes it unverified
  • C. Only under explore
  • D. Only if the comment is in the docstring
Show answer
B.
Quiz

3. Why does verify_test_stability skip the fail check for Java tests?

  • A. Java tests can’t fail
  • B. A mutant copy would duplicate the test class; use PIT instead
  • C. Java has no assertions
  • D. The check needs Python
Show answer
B.