From 307a41339cce8e7a90487bc20f778f2b66741b3c Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 12 Dec 2013 06:41:48 +0100 Subject: [PATCH] fix expicit assert messages for Python2.6: it turns out python2.6 instantiates the AssertionError differently for tuples. Test and fix to neutralize it. --- _pytest/assertion/reinterpret.py | 14 +++++++++++--- testing/test_assertion.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/_pytest/assertion/reinterpret.py b/_pytest/assertion/reinterpret.py index 88fbb31a5..fe1f87d2d 100644 --- a/_pytest/assertion/reinterpret.py +++ b/_pytest/assertion/reinterpret.py @@ -1,18 +1,26 @@ import sys import py from _pytest.assertion.util import BuiltinAssertionError +u = py.builtin._totext class AssertionError(BuiltinAssertionError): def __init__(self, *args): BuiltinAssertionError.__init__(self, *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: - self.msg = py.builtin._totext(args[0]) + self.msg = u(toprint) except Exception: - self.msg = py.builtin._totext( + self.msg = u( "<[broken __repr__] %s at %0xd>" - % (args[0].__class__, id(args[0]))) + % (toprint.__class__, id(toprint))) else: f = py.code.Frame(sys._getframe(1)) try: diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 2a18bbf01..fbe44eb73 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -386,3 +386,16 @@ def test_recursion_source_decode(testdir): result.stdout.fnmatch_lines(""" """) + +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)* + """)