3 doctest
3.0 Intro
- 为了最新测试module’s docstrings are up-to-date
- 为了回归测试regression testing by verifying that interactive examples from a test file or a test object work as expected
- 为了写指南文档To write tutorial documentation for a package, liberally illustrated with input-output examples.
官方samples:https://github.com/python/cpython/blob/3.7/Lib/test/test_doctest.py
3.1 Simple Usage in Docstrings
# module M
if __name__ == "__main__":
import doctest
doctest.testmod()
python M.py # only fail
python M.py -v # all(verbose)
doctest
then examines docstrings in module M
.
3.2 Simple Usage in a Text File
import doctest
doctest.testfile("example.txt")