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.
assertEqual() in Python is a unittest library function that is used in unit testing to check the equality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are equal assertEqual() will return true else return false.
assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return false.
Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.
If an assertion fails, then your program should crash because a condition that was supposed to be true became false. You shouldn't change this intended behavior by catching the exception with a try … except block. A proper use of assertions is to inform developers about unrecoverable errors in a program.
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
Since you're using pytest, you could also use pytest's plain assertions instead of the unittest compatibility:
def test_foo():
assert "abcdefg" == "abcde", "My message"
Output:
====================== FAILURES ======================
______________________ test_foo ______________________
def test_foo():
> assert "abcdefg" == "abcde", "My message"
E AssertionError: My message
E assert 'abcdefg' == 'abcde'
E - abcdefg
E ? --
E + abcde
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With