On this page
doctest.testfile
Prev: doctest.set_unittest_reportflags | Up: doctest | Next: doctest.testmod
Rel: doctest.debug, doctest.debug_src, doctest.DebugRunner, doctest.DocFileSuite
Ref: docs.python.org — doctest.testfile
Tags: #public #python3 #stdlib #doctest
Import
import doctest
from doctest import testfile
Signature & Overview
doctest.testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=<doctest.DocTestParser object at 0x109e196d0>, encoding=None)
Test examples in the given file. Return (#failures, #tests).
Usage
import doctest
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "guide.txt"
p.write_text("Example:\n>>> 3 + 4\n7\n", encoding="utf-8")
res = doctest.testfile(str(p), module_relative=False)
assert res.failed == 0 and res.attempted == 1
Gotchas
- Always pass
module_relative=Falsefor absolute or CWD-relative paths.
REPL Inspect
from doctest import testfile
help(testfile)
Linked from
Related notes
- tuts/python3/stdlib_module/doctest/set_unittest_reportflags Linked here; links to this page; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/testmod Linked here; links to this page; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest links to this page; shared tags: #failures, #stdlib, #tests
- tuts/python3/stdlib_module/doctest/debug Linked here; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/debug_src Linked here; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/DebugRunner Linked here; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/DocFileSuite Linked here; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/DocTest Linked here; shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/DocTestFailure shared tags: #doctest, #stdlib
- tuts/python3/stdlib_module/doctest/DocTestFinder shared tags: #doctest, #stdlib