fix expicit assert messages for Python2.6: it turns out python2.6

instantiates the AssertionError differently for tuples.  Test
and fix to neutralize it.
This commit is contained in:
holger krekel 2013-12-12 06:41:48 +01:00
parent 72ebd74715
commit 307a41339c
2 changed files with 24 additions and 3 deletions

View File

@ -1,18 +1,26 @@
import sys import sys
import py import py
from _pytest.assertion.util import BuiltinAssertionError from _pytest.assertion.util import BuiltinAssertionError
u = py.builtin._totext
class AssertionError(BuiltinAssertionError): class AssertionError(BuiltinAssertionError):
def __init__(self, *args): def __init__(self, *args):
BuiltinAssertionError.__init__(self, *args) BuiltinAssertionError.__init__(self, *args)
if args: if args:
# on Python2.6 we get len(args)==2 for: assert 0, (x,y)
# on Python2.7 and above we always get len(args) == 1
# with args[0] being the (x,y) tuple.
if len(args) > 1:
toprint = args
else:
toprint = args[0]
try: try:
self.msg = py.builtin._totext(args[0]) self.msg = u(toprint)
except Exception: except Exception:
self.msg = py.builtin._totext( self.msg = u(
"<[broken __repr__] %s at %0xd>" "<[broken __repr__] %s at %0xd>"
% (args[0].__class__, id(args[0]))) % (toprint.__class__, id(toprint)))
else: else:
f = py.code.Frame(sys._getframe(1)) f = py.code.Frame(sys._getframe(1))
try: try:

View File

@ -386,3 +386,16 @@ def test_recursion_source_decode(testdir):
result.stdout.fnmatch_lines(""" result.stdout.fnmatch_lines("""
<Module*> <Module*>
""") """)
def test_AssertionError_message(testdir):
testdir.makepyfile("""
def test_hello():
x,y = 1,2
assert 0, (x,y)
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines("""
*def test_hello*
*assert 0, (x,y)*
*AssertionError: (1, 2)*
""")