feat(py-doctest): Python tests are doctests on the API they test

Docs go stale, and a test is often the clearest statement of what code
does; a doctest is both. Setup goes in private helpers in the module
(a `_MockIO` named after `main`'s parameters, `_shown()` to sort), so
each example is a call and its output. A script's module docstring is
its manual and runs `main(...)` with fakes; otherwise the module
docstring stays brief, per docs-files-api.

The rule had lived only in one workspace's project memory. It came
from qtrx, where agents kept copying test_bld_balance.py: "test_bld_balance
is a bad precedent. I keep running into that." Hence the trigger on
being about to write a test_*.py, and lazy-tdd's step 1 now points
here, since "the project's existing framework and style" is what sent
agents to the neighboring file.

The example script's doctests pass under several PYTHONHASHSEEDs.

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-11-14 15:50:50 -06:00
1 parent 6adb4bce33
commit 5c477151cc
3 files changed
+89 -1

No files matched your search

+3 -1
View File
@@ -31,7 +31,9 @@ From Dan Connolly's
## paying the debt (test-driven debugging)
1. Write a test that reproduces the goof, in the project's existing test
framework and style, next to related tests.
framework and style, next to related tests. For a Python script or
tool, that's a doctest in the module, whatever files sit next to it
([py-doctest](../py-doctest/SKILL.md)).
2. Run it; **watch it fail** for the reason you expect. A test that
passes before the fix proves nothing about the fix.
3. Fix the code.