Sub topic 1
Tests as Executable Specifications
Prerequisites
This chapter assumes you can do the following:
- Write basic Python functions.
- Run unit tests with a framework such as pytest.
- Read a Git diff.
- Use a coding agent or AI coding assistant at a basic level.
Why a test counts as a specification
A specification says what a piece of code must do. A written description can be vague, and two readers can understand it differently. An automated test is a specification that a machine can check. It runs the code with a known input and compares the result to the expected result. If the two differ, the test fails.
This matters a lot when a coding agent writes the code. An agent cannot ask you what you meant in the way a colleague can. It reads your request, produces code, and moves on. A test gives the agent a concrete target and a pass or fail signal it can read without your help.
Anatomy of a test
A test has three parts:
- Setup: the input or starting state.
- Action: a call to the code under test.
- Assertion: a statement that the result equals what you expect. An assertion is a check that raises an error when its condition is false.
Here is a test for a function called slugify. A slug is a short, URL-safe version of a title, such as hello-world.
# test_slugify.py
from slugify_util import slugify
def test_lowercases_and_joins_words_with_hyphens():
assert slugify("Hello World") == "hello-world"The input is "Hello World". The action is the call to slugify. The assertion states the expected output. The test name describes the behavior, so the test file reads like a list of requirements.
You run the tests with one command:
python -m pytest -qpytest prints a dot for each passing test and a detailed report for each failing one. That report is the feedback an agent reads to decide its next step.
What one test does not say
One test describes one example. Many different programs satisfy a single example. The next implementation passes the test above, yet it is useless:
# A degenerate implementation
def slugify(text):
return "hello-world"It ignores its input and returns the expected string. The test is satisfied, but the behavior you wanted is missing. A human would see that this is not what was meant. An agent trying to turn a failing test green may produce exactly this code, especially when only one example exists.
The fix is to add examples that vary. Each new test narrows the set of programs that pass:
# test_slugify.py (added later)
def test_strips_punctuation():
assert slugify("Hello, World!") == "hello-world"
def test_collapses_repeated_separators():
assert slugify("a -- b") == "a-b"The hardcoded version now fails two of the three tests. Only an implementation that handles the general rule passes all of them.
Writing tests that specify well
Use these habits when you write tests that an agent will work against:
- Test behavior, not internals. Assert on the return value or visible effect. Do not assert on which helper function was called, because that blocks later refactoring.
- Name each test after one behavior. A failing test name should tell the agent and you what is missing.
- Cover several kinds of input. Include a typical case, an edge case such as empty input, and a case that exposes shortcuts.
- Keep one reason to fail. A test with many assertions hides which behavior broke.
Common errors
| Mistake | Effect |
|---|---|
| Only one example per behavior | The agent can hardcode the answer |
| Asserting on implementation details | Tests break during harmless refactors |
| Vague test names such as `test_1` | Failure output does not say what is missing |
| Tests that depend on each other or on run order | Failures are hard to trace |
Exercise
The function is_even(n) has one test: assert is_even(4) is True. Describe one implementation that passes this test but is wrong, and name two tests you would add to rule it out.
Show answer
def is_even(n): return True passes the test but is wrong. Add assert is_even(3) is False to rule out always returning True, and assert is_even(0) is True or assert is_even(-2) is True to cover an edge case and a negative number. Together they force the code to apply the real rule, not a fixed answer.