Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 877
guettli Avatar asked Apr 25 '16 07:04

guettli


People also ask

What does self assertEqual do in Python?

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.

How do you use assertTrue in Python?

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.

What does unittest main () do?

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.

What happens when Python assert fails?

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.


2 Answers

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
like image 161
falsetru Avatar answered Sep 29 '22 09:09

falsetru


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
like image 20
The Compiler Avatar answered Sep 29 '22 10:09

The Compiler