2010-11-06 18:38:53 +08:00
|
|
|
""" discover and run doctests in modules and test files."""
|
2014-08-01 06:13:40 +08:00
|
|
|
from __future__ import absolute_import
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
import traceback
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from _pytest._code.code import TerminalRepr, ReprFileLocation, ExceptionInfo
|
2015-07-13 10:43:33 +08:00
|
|
|
from _pytest.python import FixtureRequest
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_addoption(parser):
|
2014-10-08 20:31:17 +08:00
|
|
|
parser.addini('doctest_optionflags', 'option flags for doctests',
|
|
|
|
type="args", default=["ELLIPSIS"])
|
2010-01-03 19:41:29 +08:00
|
|
|
group = parser.getgroup("collect")
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption("--doctest-modules",
|
|
|
|
action="store_true", default=False,
|
2010-01-03 06:30:46 +08:00
|
|
|
help="run doctests in all .py modules",
|
2009-05-19 05:26:16 +08:00
|
|
|
dest="doctestmodules")
|
2010-01-03 06:30:46 +08:00
|
|
|
group.addoption("--doctest-glob",
|
2015-12-31 04:19:08 +08:00
|
|
|
action="append", default=[], metavar="pat",
|
2010-01-03 06:30:46 +08:00
|
|
|
help="doctests file matching pattern, default: test*.txt",
|
|
|
|
dest="doctestglob")
|
2015-02-08 14:25:23 +08:00
|
|
|
group.addoption("--doctest-ignore-import-errors",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="ignore doctest ImportErrors",
|
|
|
|
dest="doctest_ignore_import_errors")
|
2010-01-03 06:30:46 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_collect_file(path, parent):
|
2010-01-03 06:30:46 +08:00
|
|
|
config = parent.config
|
2009-05-19 05:26:16 +08:00
|
|
|
if path.ext == ".py":
|
2010-11-06 16:58:04 +08:00
|
|
|
if config.option.doctestmodules:
|
2009-05-19 05:26:16 +08:00
|
|
|
return DoctestModule(path, parent)
|
2015-12-12 03:58:49 +08:00
|
|
|
elif _is_doctest(config, path, parent):
|
2009-05-19 05:26:16 +08:00
|
|
|
return DoctestTextfile(path, parent)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2015-12-12 03:58:49 +08:00
|
|
|
def _is_doctest(config, path, parent):
|
|
|
|
if path.ext in ('.txt', '.rst') and parent.session.isinitpath(path):
|
|
|
|
return True
|
2015-12-31 04:19:08 +08:00
|
|
|
globs = config.getoption("doctestglob") or ['test*.txt']
|
2015-12-12 03:58:49 +08:00
|
|
|
for glob in globs:
|
|
|
|
if path.check(fnmatch=glob):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2009-08-26 02:24:43 +08:00
|
|
|
class ReprFailDoctest(TerminalRepr):
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def __init__(self, reprlocation, lines):
|
|
|
|
self.reprlocation = reprlocation
|
|
|
|
self.lines = lines
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def toterminal(self, tw):
|
|
|
|
for line in self.lines:
|
|
|
|
tw.line(line)
|
|
|
|
self.reprlocation.toterminal(tw)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
class DoctestItem(pytest.Item):
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2013-05-17 23:18:22 +08:00
|
|
|
def __init__(self, name, parent, runner=None, dtest=None):
|
|
|
|
super(DoctestItem, self).__init__(name, parent)
|
|
|
|
self.runner = runner
|
|
|
|
self.dtest = dtest
|
2015-10-02 09:59:37 +08:00
|
|
|
self.obj = None
|
|
|
|
self.fixture_request = None
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
if self.dtest is not None:
|
|
|
|
self.fixture_request = _setup_fixtures(self)
|
|
|
|
globs = dict(getfixture=self.fixture_request.getfuncargvalue)
|
|
|
|
self.dtest.globs.update(globs)
|
2013-05-17 23:18:22 +08:00
|
|
|
|
|
|
|
def runtest(self):
|
2015-08-26 10:12:02 +08:00
|
|
|
_check_all_skipped(self.dtest)
|
2013-05-17 23:18:22 +08:00
|
|
|
self.runner.run(self.dtest)
|
|
|
|
|
2009-07-26 00:09:01 +08:00
|
|
|
def repr_failure(self, excinfo):
|
2014-08-01 06:13:40 +08:00
|
|
|
import doctest
|
2010-12-07 02:00:30 +08:00
|
|
|
if excinfo.errisinstance((doctest.DocTestFailure,
|
|
|
|
doctest.UnexpectedException)):
|
2009-02-27 18:18:27 +08:00
|
|
|
doctestfailure = excinfo.value
|
|
|
|
example = doctestfailure.example
|
|
|
|
test = doctestfailure.test
|
2010-07-27 03:15:15 +08:00
|
|
|
filename = test.filename
|
2013-03-25 03:05:29 +08:00
|
|
|
if test.lineno is None:
|
|
|
|
lineno = None
|
|
|
|
else:
|
|
|
|
lineno = test.lineno + example.lineno + 1
|
2009-02-27 18:18:27 +08:00
|
|
|
message = excinfo.type.__name__
|
|
|
|
reprlocation = ReprFileLocation(filename, lineno, message)
|
2015-12-30 06:55:19 +08:00
|
|
|
checker = _get_checker()
|
2014-08-01 06:13:40 +08:00
|
|
|
REPORT_UDIFF = doctest.REPORT_UDIFF
|
2013-03-25 03:05:29 +08:00
|
|
|
if lineno is not None:
|
2016-01-09 08:19:37 +08:00
|
|
|
lines = doctestfailure.test.docstring.splitlines(False)
|
|
|
|
# add line numbers to the left of the error message
|
|
|
|
lines = ["%03d %s" % (i + test.lineno + 1, x)
|
|
|
|
for (i, x) in enumerate(lines)]
|
|
|
|
# trim docstring error lines to 10
|
|
|
|
lines = lines[example.lineno - 9:example.lineno + 1]
|
2013-03-25 03:05:29 +08:00
|
|
|
else:
|
2016-01-09 08:19:37 +08:00
|
|
|
lines = ['EXAMPLE LOCATION UNKNOWN, not showing all tests of that example']
|
2013-03-25 03:05:29 +08:00
|
|
|
indent = '>>>'
|
|
|
|
for line in example.source.splitlines():
|
|
|
|
lines.append('??? %s %s' % (indent, line))
|
|
|
|
indent = '...'
|
2010-12-07 02:00:30 +08:00
|
|
|
if excinfo.errisinstance(doctest.DocTestFailure):
|
|
|
|
lines += checker.output_difference(example,
|
|
|
|
doctestfailure.got, REPORT_UDIFF).split("\n")
|
|
|
|
else:
|
2015-11-27 22:43:01 +08:00
|
|
|
inner_excinfo = ExceptionInfo(excinfo.value.exc_info)
|
2010-12-07 02:00:30 +08:00
|
|
|
lines += ["UNEXPECTED EXCEPTION: %s" %
|
|
|
|
repr(inner_excinfo.value)]
|
2014-08-01 06:13:40 +08:00
|
|
|
lines += traceback.format_exception(*excinfo.value.exc_info)
|
2009-02-27 18:18:27 +08:00
|
|
|
return ReprFailDoctest(reprlocation, lines)
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2009-07-26 00:09:01 +08:00
|
|
|
return super(DoctestItem, self).repr_failure(excinfo)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def reportinfo(self):
|
2013-05-17 23:18:22 +08:00
|
|
|
return self.fspath, None, "[doctest] %s" % self.name
|
2010-09-26 22:23:44 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2014-10-08 20:31:17 +08:00
|
|
|
def _get_flag_lookup():
|
|
|
|
import doctest
|
|
|
|
return dict(DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1,
|
|
|
|
DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE,
|
|
|
|
NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE,
|
|
|
|
ELLIPSIS=doctest.ELLIPSIS,
|
|
|
|
IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL,
|
2015-08-13 08:41:31 +08:00
|
|
|
COMPARISON_FLAGS=doctest.COMPARISON_FLAGS,
|
2015-12-30 06:55:19 +08:00
|
|
|
ALLOW_UNICODE=_get_allow_unicode_flag(),
|
|
|
|
ALLOW_BYTES=_get_allow_bytes_flag(),
|
|
|
|
)
|
2014-10-08 20:31:17 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
2014-10-08 20:31:17 +08:00
|
|
|
def get_optionflags(parent):
|
|
|
|
optionflags_str = parent.config.getini("doctest_optionflags")
|
|
|
|
flag_lookup_table = _get_flag_lookup()
|
|
|
|
flag_acc = 0
|
|
|
|
for flag in optionflags_str:
|
|
|
|
flag_acc |= flag_lookup_table[flag]
|
|
|
|
return flag_acc
|
|
|
|
|
2015-08-13 08:41:31 +08:00
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
class DoctestTextfile(DoctestItem, pytest.Module):
|
2015-08-13 08:41:31 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def runtest(self):
|
2014-08-01 06:13:40 +08:00
|
|
|
import doctest
|
2015-07-13 10:43:33 +08:00
|
|
|
fixture_request = _setup_fixtures(self)
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
# inspired by doctest.testfile; ideally we would use it directly,
|
|
|
|
# but it doesn't support passing a custom checker
|
|
|
|
text = self.fspath.read()
|
|
|
|
filename = str(self.fspath)
|
|
|
|
name = self.fspath.basename
|
|
|
|
globs = dict(getfixture=fixture_request.getfuncargvalue)
|
|
|
|
if '__name__' not in globs:
|
|
|
|
globs['__name__'] = '__main__'
|
|
|
|
|
|
|
|
optionflags = get_optionflags(self)
|
|
|
|
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
|
2015-12-30 06:55:19 +08:00
|
|
|
checker=_get_checker())
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
parser = doctest.DocTestParser()
|
|
|
|
test = parser.get_doctest(text, globs, name, filename, 0)
|
2015-08-26 10:12:02 +08:00
|
|
|
_check_all_skipped(test)
|
2015-08-13 08:41:31 +08:00
|
|
|
runner.run(test)
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2015-08-26 10:12:02 +08:00
|
|
|
def _check_all_skipped(test):
|
|
|
|
"""raises pytest.skip() if all examples in the given DocTest have the SKIP
|
|
|
|
option set.
|
|
|
|
"""
|
|
|
|
import doctest
|
|
|
|
all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
|
|
|
|
if all_skipped:
|
|
|
|
pytest.skip('all tests skipped by +SKIP option')
|
|
|
|
|
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
class DoctestModule(pytest.Module):
|
2013-05-17 23:18:22 +08:00
|
|
|
def collect(self):
|
2014-08-01 06:13:40 +08:00
|
|
|
import doctest
|
2010-11-18 22:31:58 +08:00
|
|
|
if self.fspath.basename == "conftest.py":
|
2015-07-13 10:43:33 +08:00
|
|
|
module = self.config.pluginmanager._importconftest(self.fspath)
|
2010-11-18 22:31:58 +08:00
|
|
|
else:
|
2015-02-08 14:25:23 +08:00
|
|
|
try:
|
|
|
|
module = self.fspath.pyimport()
|
|
|
|
except ImportError:
|
|
|
|
if self.config.getvalue('doctest_ignore_import_errors'):
|
|
|
|
pytest.skip('unable to import module %r' % self.fspath)
|
|
|
|
else:
|
|
|
|
raise
|
2013-05-17 23:18:22 +08:00
|
|
|
# uses internal doctest module parsing mechanism
|
|
|
|
finder = doctest.DocTestFinder()
|
2015-02-08 14:25:23 +08:00
|
|
|
optionflags = get_optionflags(self)
|
2015-08-13 08:41:31 +08:00
|
|
|
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
|
2015-12-30 06:55:19 +08:00
|
|
|
checker=_get_checker())
|
2015-10-02 09:59:37 +08:00
|
|
|
for test in finder.find(module, module.__name__):
|
2015-02-08 14:25:23 +08:00
|
|
|
if test.examples: # skip empty doctests
|
2013-05-17 23:18:22 +08:00
|
|
|
yield DoctestItem(test.name, self, runner, test)
|
2015-07-13 10:43:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _setup_fixtures(doctest_item):
|
|
|
|
"""
|
2015-10-02 09:59:37 +08:00
|
|
|
Used by DoctestTextfile and DoctestItem to setup fixture information.
|
2015-07-13 10:43:33 +08:00
|
|
|
"""
|
|
|
|
def func():
|
|
|
|
pass
|
|
|
|
|
|
|
|
doctest_item.funcargs = {}
|
|
|
|
fm = doctest_item.session._fixturemanager
|
|
|
|
doctest_item._fixtureinfo = fm.getfixtureinfo(node=doctest_item, func=func,
|
|
|
|
cls=None, funcargs=False)
|
|
|
|
fixture_request = FixtureRequest(doctest_item)
|
|
|
|
fixture_request._fillfixtures()
|
|
|
|
return fixture_request
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
def _get_checker():
|
2015-08-13 08:41:31 +08:00
|
|
|
"""
|
|
|
|
Returns a doctest.OutputChecker subclass that takes in account the
|
2015-12-30 06:55:19 +08:00
|
|
|
ALLOW_UNICODE option to ignore u'' prefixes in strings and ALLOW_BYTES
|
|
|
|
to strip b'' prefixes.
|
|
|
|
Useful when the same doctest should run in Python 2 and Python 3.
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
An inner class is used to avoid importing "doctest" at the module
|
|
|
|
level.
|
|
|
|
"""
|
2015-12-30 06:55:19 +08:00
|
|
|
if hasattr(_get_checker, 'LiteralsOutputChecker'):
|
|
|
|
return _get_checker.LiteralsOutputChecker()
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
import doctest
|
|
|
|
import re
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
class LiteralsOutputChecker(doctest.OutputChecker):
|
2015-08-13 08:41:31 +08:00
|
|
|
"""
|
|
|
|
Copied from doctest_nose_plugin.py from the nltk project:
|
|
|
|
https://github.com/nltk/nltk
|
2015-12-30 06:55:19 +08:00
|
|
|
|
|
|
|
Further extended to also support byte literals.
|
2015-08-13 08:41:31 +08:00
|
|
|
"""
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
_unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
|
|
|
|
_bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE)
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
def check_output(self, want, got, optionflags):
|
2015-08-13 09:46:13 +08:00
|
|
|
res = doctest.OutputChecker.check_output(self, want, got,
|
|
|
|
optionflags)
|
2015-08-13 08:41:31 +08:00
|
|
|
if res:
|
|
|
|
return True
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
allow_unicode = optionflags & _get_allow_unicode_flag()
|
|
|
|
allow_bytes = optionflags & _get_allow_bytes_flag()
|
|
|
|
if not allow_unicode and not allow_bytes:
|
2015-08-13 08:41:31 +08:00
|
|
|
return False
|
|
|
|
|
2015-08-13 09:46:13 +08:00
|
|
|
else: # pragma: no cover
|
2015-12-30 06:55:19 +08:00
|
|
|
def remove_prefixes(regex, txt):
|
|
|
|
return re.sub(regex, r'\1\2', txt)
|
|
|
|
|
|
|
|
if allow_unicode:
|
|
|
|
want = remove_prefixes(self._unicode_literal_re, want)
|
|
|
|
got = remove_prefixes(self._unicode_literal_re, got)
|
|
|
|
if allow_bytes:
|
|
|
|
want = remove_prefixes(self._bytes_literal_re, want)
|
|
|
|
got = remove_prefixes(self._bytes_literal_re, got)
|
2015-08-13 09:46:13 +08:00
|
|
|
res = doctest.OutputChecker.check_output(self, want, got,
|
|
|
|
optionflags)
|
|
|
|
return res
|
2015-08-13 08:41:31 +08:00
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
_get_checker.LiteralsOutputChecker = LiteralsOutputChecker
|
|
|
|
return _get_checker.LiteralsOutputChecker()
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _get_allow_unicode_flag():
|
|
|
|
"""
|
|
|
|
Registers and returns the ALLOW_UNICODE flag.
|
|
|
|
"""
|
|
|
|
import doctest
|
|
|
|
return doctest.register_optionflag('ALLOW_UNICODE')
|
2015-12-30 06:55:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _get_allow_bytes_flag():
|
|
|
|
"""
|
|
|
|
Registers and returns the ALLOW_BYTES flag.
|
|
|
|
"""
|
|
|
|
import doctest
|
|
|
|
return doctest.register_optionflag('ALLOW_BYTES')
|