All courses

Tests as the definition of done

Turn 'done' into a command that passes, for example by having a failing test written first and then making it pass without editing it. Explain why this gives the agent a goal it can check itself and gives you evidence.

Builds on: Describing a task: goal, context, constraints, check, Spotting tests bent to pass, Verifying before you accept a change

Read it card by card Start the whole course

"Fixed!" But says who?

You type: "Fix the bug where discount codes don't apply at checkout." The agent edits `src/cart.py` and replies: "Fixed! Discount codes now work correctly." You try one code by hand, and it seems fine. But did it fix *your* case and every other case, without breaking anything else? Neither of you had an exact check for "fixed". You only had a feeling.

Turn 'done' into a pass

In words, "done" is a judgement call, so the agent stops when its own account *sounds* finished. A test turns done into a command: `pytest` exits 0 or it doesn't. The agent can run it after every edit and keep going until it passes. That is a goal it can check itself. For you, a pass on a test you agreed on beforehand is evidence, not a claim. The catch: the test must fail first, or it proves nothing.

Test first, then make it pass

1. Ask for **only** a test that states the right behaviour. Read it: does it say what you mean? 2. Run it. It must **fail**, and for the right reason: the wrong result, not a typo or a missing import. 3. Commit the test, so any later change to it shows up in `git diff`. 4. Ask: "Make it pass. Don't edit the test. Done = the whole suite exits 0." 5. Check that the test file is unchanged (`git diff --stat HEAD` must not list it), then rerun the tests yourself.

The discount bug, done properly

You type: "Add a test `test_ten_percent_code` to `tests/test_cart.py`: code SAVE10 on a 20.00 cart gives 18.00. Only the test, change nothing else." You read the test. It checks exactly that. You run it, and it fails: the total is 20.0 instead of 18.0. That is your bug, caught by a command. The test is now the finish line. You commit it and hand over the fix, as shown on the next card.

The hand-off and the proof

The test is committed. Read the block from (1) to (5): (1) Your hand-off: a fixed finish line and one rule. (2) The agent's first run fails with 20.0 instead of 18.0. The bug is real and the test catches it. (3) "All 24 tests pass" is only a claim so far. (4) Only `src/cart.py` is listed, so the committed test is untouched. (5) Your own run: 24 passed, exit code 0. That is your proof.
(1)
▸ You: Make test_ten_percent_code pass.
  Fix src/cart.py; don't edit tests/.
  Done means `pytest` exits 0.
(2)
⚙ Run: pytest tests/test_cart.py
FAILED test_ten_percent_code
  assert 20.0 == 18.0
⚙ Edit: src/cart.py
⚙ Run: pytest
(3)
◂ Agent: All 24 tests pass. Only
  src/cart.py changed.
(4)
$ git diff --stat HEAD
 src/cart.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(5)
$ pytest
24 passed in 0.31s
$ echo $?
0

Done is a command that passes

Before the work, pin "done" to a test that fails now. After the work, accept only if that same, unedited test passes along with all the others. The agent gets a target it can check on its own, and you get evidence instead of a claim.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.