guettli guettli - 7 months ago 23
Python Question

Python: self.assertEqual(a, b, msg) --> I want diff AND msg

If I call it like this, I see a nice diff:

self.assertEqual(a, b)


If I call it like this, I see the msg only:

self.assertEqual(a, b, msg)


Is there an easy way to show the diff AND the msg?

Implementing
assertEqual()
myself would work, but I ask myself if this is really the best way.

Plattform: Python2.7 and pytest 2.6.2.

Answer

If you set longMessage attribute True, you will see both message.

Example:

class TestFoo(unittest.TestCase):

    longMessage = True  # <--

    def test_foo(self):
        self.assertEqual(1+2, 2, 'custom message')

output:

F
======================================================================
FAIL: test_foo (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "t.py", line 6, in test_foo
    self.assertEqual(1+2, 2, 'custom message')
AssertionError: 3 != 2 : custom message

----------------------------------------------------------------------
Ran 1 test in 0.000s