all Python 3doctestfunction
doctest.register_optionflag()
building blocks
Registers a new doctest directive name and returns its bit flag, for custom output comparison rules.
Syntax
FLAG = doctest.register_optionflag(name)
The flag does nothing by itself: once registered, the directive # doctest: +NAME may appear in examples, but your own OutputChecker subclass must honour it by testing optionflags & flag. Registering the same name again returns the same bit, so calling it at import time is safe.
Examples
import doctest
IGNORE_CASE = doctest.register_optionflag("IGNORE_CASE")
print(IGNORE_CASE == doctest.register_optionflag("IGNORE_CASE")) # → True
class Checker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
if optionflags & IGNORE_CASE:
want, got = want.lower(), got.lower()
return super().check_output(want, got, optionflags)
test = doctest.DocTestParser().get_doctest(">>> 'HI' # doctest: +IGNORE_CASE\n'hi'\n", {}, "t", None, 0)
print(doctest.DocTestRunner(checker=Checker()).run(test)) # → TestResults(failed=0, attempted=1)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy