# HG changeset patch -- Bitbucket.org
# Project pytest
# URL http://bitbucket.org/hpk42/pytest/overview
# User holger krekel <hol...@merlinux.eu>
# Date 1291658430 -3600
# Node ID ea8c9c761f10bff419ff3b4472d6472e2c23f14f
# Parent  f9e54a46a0e8f53550a56927d5b9e00acf7ea310
fix issue7 - assert failure inside doctest doesn't prettyprint

unexpected exceptions are now reported within the doctest failure
representation context.

--- 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("""

--- 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)
 

--- 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
_______________________________________________
py-svn mailing list
py-svn@codespeak.net
http://codespeak.net/mailman/listinfo/py-svn

Reply via email to