Skip to content
AI Skill Index

Write the tests that would have caught this

Turns a bug you just fixed into the regression test you should have had.

Author: Editorial
Sign in to save
A bug reached production. Write the tests that would have stopped it.

The bug: [WHAT WENT WRONG]
The fix:

```
[PASTE THE FIX]
```

Produce, using [TEST FRAMEWORK]:

1. **The regression test** — fails on the old code, passes on the new. This one matters most; make it obvious from the test name what broke.
2. **The neighbours** — two or three cases near the same boundary that are probably also wrong and nobody has checked.
3. **One honest note** on what still is not covered and would need a different kind of test.

Table-driven where the cases are variations on one input. Do not test the framework, do not assert on private internals, and do not write a test that would pass against a function that returns a hard-coded value.

Highlighted parts are placeholders. Replace them before you send.

Worked example

The input is invented for this page. The output shows what the prompt asks for with that input.

Example input (invented)

The bug: The shop page says "free shipping from 50". An order of exactly 50.00 was charged 4.95 for shipping.

The fix

--- a/shipping.py
+++ b/shipping.py
@@ -1,4 +1,4 @@
 def shipping_cost(subtotal):
-    if subtotal > 50:
+    if subtotal >= 50:
         return 0
     return 4.95

Test framework: unittest, from the Python standard library

Example output

import unittest

from shipping import shipping_cost


class ShippingTest(unittest.TestCase):
    def test_exactly_50_ships_free(self):
        self.assertEqual(shipping_cost(50), 0)

    def test_one_cent_either_side(self):
        cases = [(49.99, 4.95), (50.01, 0)]
        for subtotal, expected in cases:
            with self.subTest(subtotal=subtotal):
                self.assertEqual(shipping_cost(subtotal), expected)


if __name__ == "__main__":
    unittest.main()

1. The regression test is test_exactly_50_ships_free. On the old code it fails with 4.95 != 0. On the fix it passes.

2. The neighbours are 49.99 and 50.01, one cent either side of the threshold. Both pass on the old code and on the fix. They pin the two sides, so the next edit to the comparison cannot move either one unnoticed.

3. What is still not covered. shipping_cost receives a subtotal that some other code added up. As floats, 16.70 + 16.65 + 16.65 is 49.99999999999999, so a basket with those three prices still pays 4.95 after the fix. The same prices in the order 16.65, 16.65, 16.70 add up to 50.0 and ship free. No test of this function can catch that. It needs a test one level up, where the basket adds its line items, and it is a reason to hold prices in cents.

ShareXLinkedIn

3 of the 7 other prompts filed under Coding.

Coding tools and skills

3 of the 9 tools and 3 of the 7 skills filed under Coding, the same category as this prompt.

Tool

Continue

Open-source coding agent for VS Code, JetBrains and the CLI. Cursor acquired it, and the repository is read-only.

Open source
Tool

Cursor

An editor built around AI-assisted editing.

Freemium
Tool

Devin Desktop

A full IDE with a command center for running local and cloud coding agents. It used to be called Windsurf.

Freemium
Skill

Accessibility reviewer

Reviews a component against WCAG failures that automated tools miss.

Claude Code, Cursor, Any agent
Skill

API endpoint reviewer

Checks a handler for the auth, validation and error gaps that recur.

Claude Code, Cursor, Any agent
Skill

Code reviewer

Reviews a diff for defects, and refuses to pad the list with style opinions.

Claude Code, Cursor, Any agent

Coding guides

3 of the 6 guides filed under Coding, the same category as this prompt.

Coding

Reviewing code an AI wrote

It compiles, the tests pass, and it reads well. That combination is exactly why it needs a different kind of review.

Intermediate·2 min read

Cookies

We use only the cookies this site needs to work. Optional analytics helps us see which content is worth writing more of — nothing loads until you say yes.

Read the privacy policy