QA AND TEST AUTOMATION · PART 11 · AI TOOLS FOR TESTERS · PRACTICE · 103 OF 112

Testing AI-Generated Code: Review Gates That Work

More code is written with AI, so testers need gates that do not trust it. Measured on ShopRung: AI-written cart tests, once they ran, passed 4 tests that caught none of 10 planted bugs, while two hand-written boundary tests caught the quantity bug. The gates that work: see every new test fail once, grade tests with planted bugs or mutation testing, and review against the requirement.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Green proves little
  • 4 AI tests caught 0 of 10 bugs
  • 2 boundary tests caught it
  • fail once
  • mutants
  • review gate

Notes

Green, but?

Start with the tests from the previous episode: a local model's pytest file for ShopRung's cart, after three small human fixes. Four tests passed: adding five mugs works, fifteen is refused, five more works, and an out of stock mug is refused with four oh nine. They look like reasonable cart tests. The question is whether they would ever fail.

From the pair-programming trial
llama3.2:3b, with API context
after 3 human fixes:
  4 passed, 5 failed
the 4 that pass:
  add 5 mugs → 200
  add 15 mugs → 400
  add 5 more → 200
  add 10 of an out-of-stock
    mug → 409

Grade them

Grade tests the way mutation testing does: introduce bugs and see whether the tests notice. ShopRung's bug switch gives you ten planted bugs. We turned them on one at a time. The four AI tests passed every time: zero of ten caught, including quantity limit, which is a cart bug these tests were meant to cover. The hand written API suite from lab eleven catches seven of those bugs.

10 ShopRung bugs, one at a time
quantity-limit   4 passed
stock-not-reduced 4 passed
coupon-boundary  4 passed
order-leak       4 passed
... 6 more         4 passed

bugs caught: 0 of 10
lab 11's hand-written suite:
  7 bugs caught

Why

Why did they miss it? The bug lets a cart line hold eleven. The AI tests tried five and fifteen. Fifteen is refused with or without the bug, so the test cannot fail. Two hand written tests at the actual boundary, ten is allowed and the eleventh is refused, pass on the normal shop, and the eleventh fails the moment the bug is on. Boundaries come from the requirement, not from a plausible guess.

quantity-limit: 11 is accepted
AI tests: 5 and 15
  15 is refused either way
hand-written, 2 tests:
  test_ten_is_allowed
  test_eleventh_is_refused
normal shop:  2 passed
bug on:       1 failed,
              1 passed

Gate 1

The first gate: every new test must be seen failing once. Break the behaviour on purpose, with a bug switch, a wrong value or a feature flag, run the test and watch it fail with a clear message, then restore and watch it pass. A test nobody has seen fail is not yet a test. It is the same rule as red first in test driven development, and it would have exposed all four AI tests in a minute.

Seen failing once
  • break the code or the data on purpose: a bug switch, a wrong value, a feature flag
  • run the new test: it must fail with a clear message
  • restore: it must pass
  • no red seen → not a test yet
  • same rule as TDD's "red first"

Gate 2

The second gate grades whole suites: mutation testing. A tool changes the code in small ways, a greater than into greater or equal, ten into eleven, and checks that some test fails for each change. In lab eight, tests with eighty nine percent line coverage killed only forty five of seventy one mutants. Coverage says code ran; mutants say it was checked. Run it on AI written tests, and on code AI wrote.

Mutation testing
  • a tool changes the code: > to >=, 10 to 11, a removed call
  • each change is a mutant; a test must fail for it to be "killed"
  • lab 8: 89% line coverage, 45 of 71 mutants killed
  • run it on AI-written tests and on code AI wrote
  • PIT (Java), mutmut (Python)

Gate 3

The third gate is review, with a few additions for AI. Trace every expected value to a requirement. Check that imports, endpoints and fixtures exist, because that is where inventions hide. Check boundaries are tested at the limit. Reject new skips, retries and sleeps. When AI wrote the application code, write its tests from the requirement, not from the code, or they will share its mistakes. And every change has a human owner.

Review checklist
  • every expected value traced to a requirement id
  • imports, endpoints, fixtures: do they exist?
  • boundaries tested at the limit
  • no new skips, retries, sleeps
  • tests for AI-written app code written from the requirement, not from the code
  • a named human owner

In CI

Then automate what you can. A CI job runs changed tests against planted bugs and fails if none of them fail. Mutation testing runs on changed files with a minimum score. A diff check blocks new skip, only, fix me and retry settings. Coverage stays a floor, not a goal. Some teams also label AI assisted pull requests, so reviewers know to apply the extra checks.

Pipeline gates
  • changed tests must fail on a planted bug (bug switch job)
  • mutation score on changed files, with a minimum
  • block .skip, .only, fixme, retries in diffs
  • coverage is a floor, not a goal
  • label AI-assisted pull requests

Common mistakes

Mistakes. Treating green as done. Treating coverage as proof. Generating tests from the same AI written code they are meant to check, so both share the same misunderstanding. Never running tests against a planted bug or a mutant. Guessing boundaries instead of taking them from the rules. And merging changes nobody owns.

Avoid these
  • green = done
  • coverage as proof
  • tests generated from the same AI code they test
  • no planted bug or mutant run
  • boundaries guessed, not taken from rules
  • nobody owns the change

Say it like this

In the interview: I do not trust green. Every new test must be seen failing once on a planted bug, suites are graded with mutation testing, and expected values come from the requirements, not from the code. In one trial, AI written cart tests that passed caught zero of ten planted bugs, while two hand written boundary tests caught the quantity bug immediately.

In the interview
  • "I don't trust green. Every new test must fail once on a planted
  • bug; suites are graded with mutation testing; expectations
  • come from requirements. In one trial, AI-written cart tests
  • caught 0 of 10 planted bugs, while two boundary tests caught
  • the quantity bug at once."

Interview questions

Q1Why is a green AI-written test not enough?

It proves the test runs, not that it can fail. Four passing AI-written cart tests caught 0 of 10 ShopRung bugs.

Q2What does "seen failing once" mean?

Break the behaviour on purpose (a bug switch, a wrong value), watch the new test fail with a clear message, then restore and watch it pass.

Q3Why did the AI tests miss the quantity-limit bug?

They tried 5 and 15; 15 is refused with or without the bug. Tests at the boundary (10 allowed, 11th refused) caught it.

Q4What is mutation testing?

A tool makes small changes to the code (mutants); a good suite has a test fail for each. In lab 8, 89% line coverage killed only 45 of 71 mutants.

Q5How should you test application code that AI wrote?

Write tests from the requirement, not from the code, so they do not share its mistakes; then grade them with mutants or planted bugs.

Q6What extra checks does an AI-written pull request need?

Expected values traced to requirements, imports, endpoints and fixtures that really exist, boundaries at the limit, no new skips or retries, and a human owner.

Q7Which gates can run in CI?

Changed tests against planted bugs, mutation score on changed files, blocking skip, only, fixme and retries in diffs, and coverage as a floor.

Q8Why is coverage not proof?

Coverage shows code ran during tests, not that any assertion would fail if it were wrong.