From fc4f72cb1ffd600803c1872a11cbf28f4b70e847 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 6 Dec 2010 19:00:30 +0100 Subject: [PATCH] fix issue7 - assert failure inside doctest doesn't prettyprint unexpected exceptions are now reported within the doctest failure representation context. --- CHANGELOG | 3 +++ _pytest/doctest.py | 17 +++++++++++------ testing/test_doctest.py | 19 ++++++++----------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b2ea16258..f981acb63 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ Changes between 2.0.0 and 2.0.1.dev1 ---------------------------------------------- +- fix issue10: assert failures in doctest modules. + unexpected failures in doctests will not generally + show nicer, i.e. within the doctest failing context. - fix issue9: setup/teardown functions for an xfail-marked test will report as xfail if they fail but report as normally passing (not xpassing) if they succeed. This only is true diff --git a/_pytest/doctest.py b/_pytest/doctest.py index c21c82b1c..1378544ba 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -34,7 +34,9 @@ class ReprFailDoctest(TerminalRepr): class DoctestItem(pytest.Item): def repr_failure(self, excinfo): - if excinfo.errisinstance(py.std.doctest.DocTestFailure): + doctest = py.std.doctest + if excinfo.errisinstance((doctest.DocTestFailure, + doctest.UnexpectedException)): doctestfailure = excinfo.value example = doctestfailure.example test = doctestfailure.test @@ -50,12 +52,15 @@ class DoctestItem(pytest.Item): for line in filelines[i:lineno]: lines.append("%03d %s" % (i+1, line)) i += 1 - lines += checker.output_difference(example, - doctestfailure.got, REPORT_UDIFF).split("\n") + 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)] + return ReprFailDoctest(reprlocation, lines) - elif excinfo.errisinstance(py.std.doctest.UnexpectedException): - excinfo = py.code.ExceptionInfo(excinfo.value.exc_info) - return super(DoctestItem, self).repr_failure(excinfo) else: return super(DoctestItem, self).repr_failure(excinfo) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index fb7bb82e6..ed9cca9fd 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -48,19 +48,16 @@ class TestDoctests: def test_doctest_unexpected_exception(self, testdir): p = testdir.maketxtfile(""" >>> i = 0 - >>> i = 1 - >>> x + >>> 0 / i 2 """) - reprec = testdir.inline_run(p) - call = reprec.getcall("pytest_runtest_logreport") - assert call.report.failed - assert call.report.longrepr - # XXX - #testitem, = items - #excinfo = pytest.raises(Failed, "testitem.runtest()") - #repr = testitem.repr_failure(excinfo, ("", "")) - #assert repr.reprlocation + result = testdir.runpytest("--doctest-modules") + result.stdout.fnmatch_lines([ + "*unexpected_exception*", + "*>>> i = 0*", + "*>>> 0 / i*", + "*UNEXPECTED*ZeroDivision*", + ]) def test_doctestmodule(self, testdir): p = testdir.makepyfile("""