Fix doctests for pytest 4.0.1
import sys
-import traceback
import py
@@ -28,50 +27,20 @@ class LoadedModule(_pytest.python.Module):
return sys.modules[module_name]
-class DoctestItem(_pytest.doctest.DoctestItem):
- def repr_failure(self, excinfo):
- import doctest
- if excinfo.errisinstance((doctest.DocTestFailure, doctest.UnexpectedException)):
- doctestfailure = excinfo.value
- example = doctestfailure.example
- test = doctestfailure.test
- filename = test.filename
- if test.lineno is None:
- lineno = None
- else:
- lineno = test.lineno + example.lineno + 1
- message = excinfo.type.__name__
- reprlocation = _pytest.doctest.ReprFileLocation(filename, lineno, message)
- checker = doctest.OutputChecker()
- lines = []
- indent = '>>>'
- for line in example.source.splitlines():
- lines.append('%s %s' % (indent, line))
- indent = '...'
- if excinfo.errisinstance(doctest.DocTestFailure):
- lines += checker.output_difference(example, doctestfailure.got, doctest.REPORT_UDIFF).split("\n")
- else:
- inner_excinfo = py.code.ExceptionInfo(excinfo.value.exc_info)
- lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
- lines += traceback.format_exception(*excinfo.value.exc_info)
- return _pytest.doctest.ReprFailDoctest(reprlocation, lines)
- else:
- return super(DoctestItem, self).repr_failure(excinfo)
-
-
class DoctestModule(LoadedModule):
def collect(self):
- if sys.version_info[0] > 2: # XXX Fix doctests for python3
- return
import doctest
- finder = doctest.DocTestFinder()
+
module = self._getobj()
+ # uses internal doctest module parsing mechanism
+ finder = doctest.DocTestFinder()
optionflags = _pytest.doctest.get_optionflags(self)
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags)
+
for test in finder.find(module, self.name[:-(len(".py"))]):
if test.examples: # skip empty doctests
- yield DoctestItem(test.name, self, runner, test)
+ yield _pytest.doctest.DoctestItem(test.name, self, runner, test)
class CollectionPlugin(object):
