[py-svn] commit/pytest: hpk42: fix issue30 - better handling and reporting of errors in xfail expressions

Bitbucket commits-noreply at bitbucket.org
Thu Mar 3 12:19:30 CET 2011


1 new changeset in pytest:

http://bitbucket.org/hpk42/pytest/changeset/dedf96819b5c/
changeset:   r2164:dedf96819b5c
user:        hpk42
date:        2011-03-03 12:19:17
summary:     fix issue30 - better handling and reporting of errors in xfail expressions
affected #:  4 files (1.6 KB)

--- a/CHANGELOG	Wed Mar 02 18:03:43 2011 +0100
+++ b/CHANGELOG	Thu Mar 03 12:19:17 2011 +0100
@@ -1,10 +1,13 @@
 Changes between 2.0.1 and 2.0.2
 ----------------------------------------------
 
+- fix issue30 - better handling and error reporting for errors in xfail
+  expressions
+
 - fix issue28 - setup_method and pytest_generate_tests work together
 
-- fix issue24 - pytest_assertrepr_compare produces an in-line
-  exception on python3
+- fix issue23 - tmpdir argument now works on Python3.2 and WindowsXP
+  (which apparently starts to offer os.symlink now)
 
 - fixed some typos in the docs (thanks Victor)
 


--- a/_pytest/skipping.py	Wed Mar 02 18:03:43 2011 +0100
+++ b/_pytest/skipping.py	Thu Mar 03 12:19:17 2011 +0100
@@ -1,6 +1,7 @@
 """ support for skip/xfail functions and markers. """
 
 import py, pytest
+import sys
 
 def pytest_addoption(parser):
     group = parser.getgroup("general")
@@ -32,7 +33,28 @@
         return bool(self.holder)
     __nonzero__ = __bool__
 
+    def wasvalid(self):
+        return not hasattr(self, 'exc')
+
     def istrue(self):
+        try:
+            return self._istrue()
+        except KeyboardInterrupt:
+            raise
+        except:
+            self.exc = sys.exc_info()
+            if isinstance(self.exc[1], SyntaxError):
+                msg = [" " * (self.exc[1].offset + 4) + "^",]
+                msg.append("SyntaxError: invalid syntax")
+            else:
+                msg = py.std.traceback.format_exception_only(*self.exc[:2])
+            pytest.fail("Error evaluating %r expression\n"
+                        "    %s\n"
+                        "%s"
+                        %(self.name, self.expr, "\n".join(msg)),
+                        pytrace=False)
+
+    def _istrue(self):
         if self.holder:
             d = {'os': py.std.os, 'sys': py.std.sys, 'config': self.item.config}
             if self.holder.args:
@@ -99,16 +121,17 @@
             return rep
     rep = __multicall__.execute()
     evalxfail = item._evalxfail
-    if not item.config.option.runxfail and evalxfail.istrue():
-        if call.excinfo:
-            rep.outcome = "skipped"
-            rep.keywords['xfail'] = evalxfail.getexplanation()
-        elif call.when == "call":
-            rep.outcome = "failed"
-            rep.keywords['xfail'] = evalxfail.getexplanation()
-    else:
-        if 'xfail' in rep.keywords:
-            del rep.keywords['xfail']
+    if not item.config.option.runxfail:
+        if evalxfail.wasvalid() and evalxfail.istrue():
+            if call.excinfo:
+                rep.outcome = "skipped"
+                rep.keywords['xfail'] = evalxfail.getexplanation()
+            elif call.when == "call":
+                rep.outcome = "failed"
+                rep.keywords['xfail'] = evalxfail.getexplanation()
+            return rep
+    if 'xfail' in rep.keywords:
+        del rep.keywords['xfail']
     return rep
 
 # called by terminalreporter progress reporting
@@ -179,7 +202,8 @@
     except KeyError:
         #import sys
         #print >>sys.stderr, ("cache-miss: %r" % expr)
-        config._evalcache[expr] = x = eval(expr, d)
+        exprcode = py.code.compile(expr, mode="eval")
+        config._evalcache[expr] = x = eval(exprcode, d)
         return x
 
 


--- a/setup.py	Wed Mar 02 18:03:43 2011 +0100
+++ b/setup.py	Thu Mar 03 12:19:17 2011 +0100
@@ -29,7 +29,7 @@
         author='holger krekel, Guido Wesdorp, Carl Friedrich Bolz, Armin Rigo, Maciej Fijalkowski & others',
         author_email='holger at merlinux.eu',
         entry_points= make_entry_points(),
-        install_requires=['py>1.4.0'],
+        install_requires=['py>1.4.1'],
         classifiers=['Development Status :: 5 - Production/Stable',
                      'Intended Audience :: Developers',
                      'License :: OSI Approved :: MIT License',
@@ -67,4 +67,4 @@
     return {'console_scripts': l}
 
 if __name__ == '__main__':
-    main()
\ No newline at end of file
+    main()


--- a/testing/test_skipping.py	Wed Mar 02 18:03:43 2011 +0100
+++ b/testing/test_skipping.py	Thu Mar 03 12:19:17 2011 +0100
@@ -470,3 +470,31 @@
         "XPASS*test_3*",
         "SKIP*four*",
     ])
+
+def test_errors_in_xfail_skip_expressions(testdir):
+    testdir.makepyfile("""
+        import pytest
+        @pytest.mark.skipif("asd")
+        def test_nameerror():
+            pass
+        @pytest.mark.xfail("syntax error")
+        def test_syntax():
+            pass
+
+        def test_func():
+            pass
+    """)
+    result = testdir.runpytest()
+    result.stdout.fnmatch_lines([
+        "*ERROR*test_nameerror*",
+        "*evaluating*skipif*expression*",
+        "*asd*",
+        "*ERROR*test_syntax*",
+        "*evaluating*xfail*expression*",
+        "    syntax error",
+        "                ^",
+        "SyntaxError: invalid syntax",
+        "*1 pass*2 error*",
+    ])
+
+

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the pytest-commit mailing list