[Solved]-Parameter order for unittest.TestCase.assertEqual


I typically use assertEqual(expected, actual) because the output message makes more sense.


from unittest import TestCase

class TestClass(TestCase):
    def test_equal(self):
        expected = 1
        actual = 2
        self.assertEqual(expected, actual)


2 != 1

Expected :1
Actual   :2

If order is reversed, then the message will incorrectly say:

Expected :2
Actual   :1


As per the assertEqual document:

Test that first and second are equal. If the values do not compare equal, the test will fail.

So, you may put expected and actual at any place and it will return the same result. But it is the common practice to use actual result as the first argument and expected result as the second argument. It is also demonstrated in Python 3’s Unittest Example:

s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])


The format of the failure message for assertions like assertGreaterEqual(a, b) indicates the order in Python unitest for these kind of assertions is (actual, expected), because it expects that a >= b, in other words, that the actual value is greater than or equal to the expected value. Your assertion failure messages will be confusing unless you adopt the correct order.

PHPUnit and other jUnit-based testing frameworks on the other hand typically use (expected, actual)

Leave a comment