All courses

Workflow: having the agent write tests

Get useful tests for existing code by asking for behaviour-focused cases, edge cases and failure cases. Check that each test would fail if the code were broken, and decide what to do when a test exposes a real bug.

Builds on: Spotting tests bent to pass, Verifying before you accept a change

Read it card by card Start the whole course

You ask for tests and get eight passes

Your project has `src/pricing.py` with a function `apply_discount(price, percent)` and no tests. You type: ▸ You: write tests for pricing.py A minute later: ◂ Agent: Added 8 tests in `tests/test_pricing.py`. All 8 pass. Eight passing tests feel like safety. But do they check what the function *should* do, or only what it happens to do today?

Tests copied from the code copy its bugs

To write tests, the agent reads the function and describes it. If the code returns a negative price for a 200% discount, a test written from the code may expect that negative price. Expected values also often get computed with the code's own formula, so the test can never disagree with it. Useful tests come from **intent**: what *should* happen. You hold that knowledge, so give it to the agent.

A routine for agent-written tests

1. Commit first, so `git status` is clean. 2. State the intended behaviour. List edge cases (empty, zero, limits) and failure cases (bad input). Forbid edits to the code being tested. 3. Read each test. Expected values should be plain numbers, like `== 45.0`. 4. **Break check:** make the code wrong, run the tests, and expect failures. Then undo. 5. A failing test is your decision: is the code wrong, or the test?

Break check, then a real bug

With the better request (next card), the agent replies: ◂ Agent: 9 pass, 1 fails: `test_rejects_over_100`. `apply_discount(50, 200)` returns -50.0, not `ValueError`. Break check: change `1 - percent / 100` to `1 + percent / 100` and run `pytest`. The normal cases and the 100% case now fail. The 0%, price-0 and below-0 tests still pass, because this change can't affect them. Undo it; `git diff src/pricing.py` is empty. The failing test found a real bug. Keep the test and fix the code as a separate, reviewed change.

A request that gets useful tests

Behaviour, edge cases, failure cases, plain expected values, and a ban on bending anything.
▸ You: Add tests in tests/test_pricing.py
for apply_discount in src/pricing.py.
It should return price minus percent%,
rounded to 2 decimals.
Cover: normal cases; edge cases (0%,
100%, price 0); bad input (percent below
0 or above 100 must raise ValueError).
Write expected values as plain numbers,
e.g. == 45.0, not recomputed formulas.
Call the real function; no mocks.
Do not edit src/pricing.py.
If a test fails, don't change it: stop
and tell me which test and what it got.
Run pytest and show me the output.

Remember

Tell the agent what the code *should* do, including edge and failure cases, and forbid edits to that code. Then break the code on purpose. A test that can't fail proves nothing. A test that fails for a real reason is a bug for you to decide on, never something to edit away.

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.