Skip to content

Commit

Permalink
Fix doctests for pytest 4.0.1
Browse files Browse the repository at this point in the history
ref:e31e67704ea29a2a3582fa688a5afe8ba9e00f6e
  • Loading branch information
mityada committed Dec 14, 2018
1 parent 71bb53a commit 306fbbe
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions library/python/pytest/plugins/collection.py
@@ -1,5 +1,4 @@
import sys
import traceback

import py

Expand Down Expand Up @@ -28,50 +27,20 @@ def _getobj(self):
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):
Expand Down

0 comments on commit 306fbbe

Please sign in to comment.