2010-11-06 18:38:53 +08:00
|
|
|
""" discover and run doctests in modules and test files."""
|
2009-05-21 05:12:37 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest, py
|
2013-01-31 00:32:37 +08:00
|
|
|
from _pytest.python import FixtureRequest, FuncFixtureInfo
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._code.code import TerminalRepr, ReprFileLocation
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_addoption(parser):
|
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",
|
|
|
|
action="store", default="test*.txt", metavar="pat",
|
|
|
|
help="doctests file matching pattern, default: test*.txt",
|
|
|
|
dest="doctestglob")
|
|
|
|
|
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)
|
2010-11-17 21:33:21 +08:00
|
|
|
elif (path.ext in ('.txt', '.rst') and parent.session.isinitpath(path)) or \
|
|
|
|
path.check(fnmatch=config.getvalue("doctestglob")):
|
2009-05-19 05:26:16 +08:00
|
|
|
return DoctestTextfile(path, parent)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-08-26 02:24:43 +08:00
|
|
|
class ReprFailDoctest(TerminalRepr):
|
2009-02-27 18:18:27 +08:00
|
|
|
def __init__(self, reprlocation, lines):
|
|
|
|
self.reprlocation = reprlocation
|
|
|
|
self.lines = lines
|
|
|
|
def toterminal(self, tw):
|
|
|
|
for line in self.lines:
|
|
|
|
tw.line(line)
|
|
|
|
self.reprlocation.toterminal(tw)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
class DoctestItem(pytest.Item):
|
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
|
|
|
|
|
|
|
|
def runtest(self):
|
|
|
|
self.runner.run(self.dtest)
|
|
|
|
|
2009-07-26 00:09:01 +08:00
|
|
|
def repr_failure(self, excinfo):
|
2010-12-07 02:00:30 +08:00
|
|
|
doctest = py.std.doctest
|
|
|
|
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)
|
2010-10-12 18:19:00 +08:00
|
|
|
checker = py.std.doctest.OutputChecker()
|
|
|
|
REPORT_UDIFF = py.std.doctest.REPORT_UDIFF
|
2009-02-27 18:18:27 +08:00
|
|
|
filelines = py.path.local(filename).readlines(cr=0)
|
|
|
|
lines = []
|
2013-03-25 03:05:29 +08:00
|
|
|
if lineno is not None:
|
|
|
|
i = max(test.lineno, max(0, lineno - 10)) # XXX?
|
|
|
|
for line in filelines[i:lineno]:
|
|
|
|
lines.append("%03d %s" % (i+1, line))
|
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
lines.append('EXAMPLE LOCATION UNKNOWN, not showing all tests of that example')
|
|
|
|
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:
|
|
|
|
inner_excinfo = py.code.ExceptionInfo(excinfo.value.exc_info)
|
|
|
|
lines += ["UNEXPECTED EXCEPTION: %s" %
|
|
|
|
repr(inner_excinfo.value)]
|
2011-05-28 20:38:15 +08:00
|
|
|
lines += py.std.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
|
|
|
|
2010-11-18 01:24:28 +08:00
|
|
|
class DoctestTextfile(DoctestItem, pytest.File):
|
2009-02-27 18:18:27 +08:00
|
|
|
def runtest(self):
|
2010-11-18 21:56:16 +08:00
|
|
|
doctest = py.std.doctest
|
2013-01-31 00:32:37 +08:00
|
|
|
# satisfy `FixtureRequest` constructor...
|
|
|
|
self.funcargs = {}
|
2013-11-22 22:35:20 +08:00
|
|
|
fm = self.session._fixturemanager
|
|
|
|
def func():
|
|
|
|
pass
|
|
|
|
self._fixtureinfo = fm.getfixtureinfo(node=self, func=func,
|
|
|
|
cls=None, funcargs=False)
|
2013-01-31 00:32:37 +08:00
|
|
|
fixture_request = FixtureRequest(self)
|
2013-11-22 22:35:20 +08:00
|
|
|
fixture_request._fillfixtures()
|
2010-11-18 21:56:16 +08:00
|
|
|
failed, tot = doctest.testfile(
|
2010-10-14 00:41:53 +08:00
|
|
|
str(self.fspath), module_relative=False,
|
2010-11-18 21:56:16 +08:00
|
|
|
optionflags=doctest.ELLIPSIS,
|
2013-03-21 19:04:14 +08:00
|
|
|
extraglobs=dict(getfixture=fixture_request.getfuncargvalue),
|
2010-10-14 00:41:53 +08:00
|
|
|
raise_on_error=True, verbose=0)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2013-05-17 23:18:22 +08:00
|
|
|
class DoctestModule(pytest.File):
|
|
|
|
def collect(self):
|
2010-11-18 21:56:16 +08:00
|
|
|
doctest = py.std.doctest
|
2010-11-18 22:31:58 +08:00
|
|
|
if self.fspath.basename == "conftest.py":
|
|
|
|
module = self.config._conftest.importconftest(self.fspath)
|
|
|
|
else:
|
|
|
|
module = self.fspath.pyimport()
|
2013-03-21 00:32:48 +08:00
|
|
|
# satisfy `FixtureRequest` constructor...
|
|
|
|
self.funcargs = {}
|
|
|
|
self._fixtureinfo = FuncFixtureInfo((), [], {})
|
|
|
|
fixture_request = FixtureRequest(self)
|
2013-05-17 23:18:22 +08:00
|
|
|
doctest_globals = dict(getfixture=fixture_request.getfuncargvalue)
|
|
|
|
# uses internal doctest module parsing mechanism
|
|
|
|
finder = doctest.DocTestFinder()
|
|
|
|
runner = doctest.DebugRunner(verbose=0, optionflags=doctest.ELLIPSIS)
|
|
|
|
for test in finder.find(module, module.__name__,
|
|
|
|
extraglobs=doctest_globals):
|
|
|
|
if test.examples: # skip empty doctests
|
|
|
|
yield DoctestItem(test.name, self, runner, test)
|