transition2.aiStart a project

Software Factories & AI-Assisted Engineering

Negative Test Cases for AI-Built Features: A Checklist

Agents write the happy path well. The cases that hurt are the ones nobody listed: reuse, expiry, wrong owner, wrong amount, wrong device.

Eric MacDougall ·

Answer first

For every acceptance criterion in an AI-built feature, write at least one negative case: something the system must refuse, with the exact check that proves it. Then run your area's checklist (access, deletion, money, devices) against the spec before any code is written. Agents build and test the path to the goal; refusals only get tested if the spec names them.

Why the happy path is not enough

Give an agent "let a signed-in person approve an agent's sign-in" and it will build approve, redeem and a passing test. Whether the same link works twice, whether it works after it expires, and whether it works for someone else are separate questions. The code that answers them is small, but nobody writes it unless the question is on the page.

This is the same logic as Specs With Two Reviewers for AI Agents: a spec that lists criteria, negative cases and how each is verified gives reviewers something concrete to check. This post is the checklist for the negative half.

The checklist by area

Access and sign-in

  • A second use of a single-use link or code.
  • An expired request, including one clicked later after the clock was changed.
  • Approval by a different person, or a lookalike address.
  • A valid approval used from a different device or session.
  • A request that is approved before it exists.
  • A pre-registered account that the real owner has not yet claimed.

Deletion

  • Deleting something that has not been backed up or pushed.
  • Deleting with no reason or no owner recorded.
  • A path that resolves outside the allowed directory, including through a link.
  • A bulk delete whose count is far larger than the request implied.
  • Restart or retry halfway through: does a second run delete more?

Money

  • Zero, negative, fractional and very large amounts.
  • Currency mismatch and rounding.
  • The same request sent twice with the same idempotency key, and with a different key.
  • A refund larger than the original charge.
  • A reply that is lost after the charge went through.

Devices and sessions

  • A session issued to the wrong device.
  • Two devices racing for one resource.
  • A stale screen submitting after the state changed underneath it.
  • A client that is offline during the approval.

Release

  • A rollback that was never run on a copy first.
  • A gate that passes when zero tests ran.
  • A deploy of a commit that differs from the one reviewed.

A linter that refuses a spec with no negatives

We wrote a spec in the plain form below and a short checker for it. Each acceptance criterion (AC) names how it is verified; each negative case (NEG) points at the criterion it attacks and names its own check. A criterion that is itself a refusal is marked [refusal] and is the only kind exempt from needing a negative.

AC-1: A signed-in human can approve a pending agent sign-in. Verify by: test_happy_path.
AC-2: An approved request gives the agent one session. Verify by: test_redeem_twice.
AC-3 [refusal]: A pending request expires. Verify by: test_expired.
NEG-1 (AC-1): a different signed-in person cannot approve. Verify by: test_other_signed_in_person_cannot_approve.
NEG-2 (AC-2): a second redeem is refused (replay). Verify by: test_redeem_twice.
NEG-3 (AC-2): another device cannot redeem. Verify by: test_other_device.
NEG-4 (AC-3): an expired request cannot be revived. Verify by: test_expired_cannot_be_revived.

The checker fails a spec when a criterion has no check, when an unmarked criterion has no negative case, or when the chosen domain checklist (access: wrong person, wrong device, expiry, reuse) is not covered by a negative line. The domain words are matched only against NEG lines, so a word in a test name or a criterion cannot satisfy the check by accident. Run on the spec above with the access domain:

spec ok

We then deleted NEG-2, the replay case. The checker reported:

domain access: no negative case for reuse

Nine unit tests passed: a good spec, an unmarked criterion with no negative (number 3 has no special treatment), removing the replay case, removing the device case, replacing the replay case with an unrelated one that borrows its test name, the marker rule, a negative that points at a criterion that does not exist, and a missing check. The linter still only checks structure. It cannot tell whether the negative cases are the right ones.

Step-by-step approach

  1. Write the outcome and each acceptance criterion with its check.
  2. For each criterion, write the refusal that would hurt most if it were missing.
  3. Pull the checklist for the area: access, deletion, money, devices or release.
  4. Mark anything you decline to cover, with a reason.
  5. Have a second reviewer look only for missing negatives.
  6. Run the checks against the code. Prove at least one fails when the guarded behavior is removed.
  7. Keep the spec with the change so a later reader can see what was promised.

Risks and limits

  • A checklist can become boilerplate. Use only the area you are changing, and drop items that do not apply.
  • A named negative case can still be tested badly. Break the guarded line on purpose and confirm the test fails.
  • This is a spec discipline, not a security audit. For anything exposed to attackers use a standard such as the OWASP ASVS and a qualified reviewer.
  • The sample spec and linter are small examples. We did not run them against a real project.

FAQ

What is a negative test case?
A test that proves the system refuses something it should refuse: a reused link, an expired request, a wrong owner, an amount out of range. It passes when the system says no and nothing changes.
Why do AI-written features need them listed up front?
An agent given a goal builds the path that achieves it and tests that path. Refusals are not part of the goal unless the spec names them, so they are the cases most often missing.
How many negative cases are enough?
At least one per acceptance criterion, then the domain checklist for the area you are touching. Depth should follow what a mistake does next: money, deletion and access need the most.
Can a linter check the quality of negative cases?
No. A linter can check that they exist and name how they are verified. A person still has to judge whether they are the right cases.

Sources