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
|
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):
|
2009-07-26 00:09:01 +08:00
|
|
|
def repr_failure(self, excinfo):
|
2010-10-12 18:19:00 +08:00
|
|
|
if excinfo.errisinstance(py.std.doctest.DocTestFailure):
|
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
|
2009-07-01 21:24:19 +08:00
|
|
|
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)
|
2010-07-27 03:15:15 +08:00
|
|
|
i = max(test.lineno, max(0, lineno - 10)) # XXX?
|
2009-02-27 18:18:27 +08:00
|
|
|
lines = []
|
|
|
|
for line in filelines[i:lineno]:
|
|
|
|
lines.append("%03d %s" % (i+1, line))
|
|
|
|
i += 1
|
2010-07-27 03:15:15 +08:00
|
|
|
lines += checker.output_difference(example,
|
2009-02-27 18:18:27 +08:00
|
|
|
doctestfailure.got, REPORT_UDIFF).split("\n")
|
|
|
|
return ReprFailDoctest(reprlocation, lines)
|
2010-10-12 18:19:00 +08:00
|
|
|
elif excinfo.errisinstance(py.std.doctest.UnexpectedException):
|
2009-02-27 18:18:27 +08:00
|
|
|
excinfo = py.code.ExceptionInfo(excinfo.value.exc_info)
|
2009-07-26 00:09:01 +08:00
|
|
|
return super(DoctestItem, self).repr_failure(excinfo)
|
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):
|
|
|
|
return self.fspath, None, "[doctest]"
|
|
|
|
|
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
|
|
|
|
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,
|
2010-10-14 00:41:53 +08:00
|
|
|
raise_on_error=True, verbose=0)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-11-18 01:24:28 +08:00
|
|
|
class DoctestModule(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
|
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()
|
2010-11-18 21:56:16 +08:00
|
|
|
failed, tot = doctest.testmod(
|
|
|
|
module, raise_on_error=True, verbose=0,
|
|
|
|
optionflags=doctest.ELLIPSIS)
|