On this page
  1. doctest.testfile
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from doctest import testfile
help(testfile)

Linked from

Related notes