From 93da606763a16a6964625f28a6434e2cefb99a35 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 24 Mar 2013 20:05:29 +0100 Subject: [PATCH] fix Issue 274 - dont fail when doctest does not know the example location instead only the last test is shown, this could use some further enhancement --- CHANGELOG | 3 +++ _pytest/doctest.py | 20 +++++++++++++++----- testing/test_doctest.py | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 52ebc80ce..00001b58d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ Changes between 2.3.4 and 2.3.5dev ----------------------------------- +- Issue 274 - dont try to show full doctest example + when doctest does not know the example location + - issue 280 - disable assertion rewriting on buggy CPython 2.6.0 - inject "getfixture()" helper to retrieve fixtures from doctests, diff --git a/_pytest/doctest.py b/_pytest/doctest.py index 6753e5040..702315883 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -42,17 +42,27 @@ class DoctestItem(pytest.Item): example = doctestfailure.example test = doctestfailure.test filename = test.filename - lineno = test.lineno + example.lineno + 1 + if test.lineno is None: + lineno = None + else: + lineno = test.lineno + example.lineno + 1 message = excinfo.type.__name__ reprlocation = ReprFileLocation(filename, lineno, message) checker = py.std.doctest.OutputChecker() REPORT_UDIFF = py.std.doctest.REPORT_UDIFF filelines = py.path.local(filename).readlines(cr=0) - i = max(test.lineno, max(0, lineno - 10)) # XXX? lines = [] - for line in filelines[i:lineno]: - lines.append("%03d %s" % (i+1, line)) - i += 1 + 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 = '...' if excinfo.errisinstance(doctest.DocTestFailure): lines += checker.output_difference(example, doctestfailure.got, REPORT_UDIFF).split("\n") diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 7b22e906c..564c675e6 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -59,6 +59,26 @@ class TestDoctests: "*UNEXPECTED*ZeroDivision*", ]) + def test_doctest_linedata_missing(self, testdir): + testdir.tmpdir.join('hello.py').write(py.code.Source(""" + class Fun(object): + @property + def test(self): + ''' + >>> a = 1 + >>> 1/0 + ''' + """)) + result = testdir.runpytest("--doctest-modules") + result.stdout.fnmatch_lines([ + "*hello*", + "*EXAMPLE LOCATION UNKNOWN, not showing all tests of that example*", + "*1/0*", + "*UNEXPECTED*ZeroDivision*", + "*1 failed*", + ]) + + def test_doctest_unex_importerror(self, testdir): testdir.tmpdir.join("hello.py").write(py.code.Source(""" import asdalsdkjaslkdjasd