def run_doctest(infile="-", txt2code=True,
globs={}, verbose=False, optionflags=0, **keyw):
"""run doctest on the text source
"""
# Allow imports from the current working dir by prepending an empty string to
# sys.path (see doc of sys.path())::
sys.path.insert(0, '')
# Import classes from the doctest module::
from doctest import DocTestParser, DocTestRunner
# Read in source. Make sure it is in text format, as tests in comments are not
# found by doctest::
(data, out_stream) = open_streams(infile, "-")
if txt2code is False:
keyw.update({'add_missing_marker': False})
converter = Code2Text(data, **keyw)
docstring = str(converter)
else:
docstring = data.read()
# decode doc string if there is a "magic comment" in the first or second line
# (http://docs.python.org/reference/lexical_analysis.html#encoding-declarations)
# ::
firstlines = ' '.join(docstring.splitlines()[:2])
match = re.search('coding[=:]\s*([-\w.]+)', firstlines)
if match:
docencoding = match.group(1)
docstring = docstring.decode(docencoding)
# Use the doctest Advanced API to run all doctests in the source text::
test = DocTestParser().get_doctest(docstring, globs, name="",
filename=infile, lineno=0)
runner = DocTestRunner(verbose, optionflags)
runner.run(test)
runner.summarize
# give feedback also if no failures occurred
if not runner.failures:
print("%d failures in %d tests"%(runner.failures, runner.tries))
return runner.failures, runner.tries
# diff
# ~~~~
#
# ::
评论列表
文章目录