doctest in Python
Examples in the docstring that run as tests.
def stars(tpm, target=90):
"""Stars earned for a run.
>>> stars(104)
3
>>> stars(70)
1
>>> [stars(t) for t in (60, 95)]
[1, 3]
"""
return 3 if tpm >= target else 1
if __name__ == "__main__":
import doctest
print(doctest.testmod())
How it works
- The expected output follows the prompt line.
testmodruns every docstring in the module.- It keeps the documentation honest, which is the point.
Keywords and builtins used here
defelseifprintreturnstars
The run, in numbers
- Lines
- 17
- Characters to type
- 234
- Tokens
- 44
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 24 seconds.
Step 3 of 3 in Selecting & faking, step 8 of 9 in Testing with pytest.