all Python 3doctestfunction
doctest.DocFileSuite()
unittest integration
Collects doctests from text files into a unittest.TestSuite, one test case per file.
Syntax
doctest.DocFileSuite(*paths, module_relative=True, package=None, setUp=None, tearDown=None, globs=None, optionflags=0, encoding=None)
As with testfile(), paths are relative to the calling module's folder by default; use module_relative=False for paths relative to the working directory. One file is one test: any failing example fails the whole file. setUp and tearDown receive the DocTest object, so test.globs can be used to inject variables.
Examples
import doctest, io, unittest
with open("a.txt", "w", encoding="utf-8") as f:
f.write(">>> base + 1\n11\n")
def set_up(test):
test.globs["base"] = 10
suite = doctest.DocFileSuite("a.txt", module_relative=False, setUp=set_up, encoding="utf-8")
result = unittest.TextTestRunner(stream=io.StringIO()).run(suite)
print(result.testsRun, result.wasSuccessful()) # → 1 True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy