Why are assertEquals() parameters in the order (expected, actual)?

前端 未结 7 1805
余生分开走
余生分开走 2021-01-31 01:28

Why do so many assertEquals() or similar function take the expected value as first parameter and the actual one as second ?
This seems counter-intuitive to me,

相关标签:
7条回答
  • 2021-01-31 01:55

    I agree with the consensus that consistency is #1, but the behavior of comparing dictionaries may be a helpful datapoint if you're evaluating this question.

    When I see a "+" on a diff, I read this as "the procedure being tested added this." Again, personal preferences apply.

    Note: I used alphabetized keys and made the dictionary longer so that only a middle key would change for clarity of the example. Other scenarios display more obfuscated diffs. Also noteworthy, assertEqual uses assertDictEqual in >=2.7 and >=3.1

    exl.py

    from unittest import TestCase
    
    
    class DictionaryTest(TestCase):
    
        def test_assert_order(self):
            self.assertEqual(
                {
                    'a_first_key': 'value',
                    'key_number_2': 'value',
                    'z_last_key': 'value',
                    'first_not_second': 'value',
                },
                {
                    'a_first_key': 'value',
                    'key_number_2': 'value',
                    'z_last_key': 'value',
                    'second_not_first': 'value',
                }
            )
    

    Output:

    $ python -m unittest exl
    F
    ======================================================================
    FAIL: test_assert_order (exl.DictionaryTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "exl.py", line 18, in test_assert_order
        'second_not_first': 'value',
    AssertionError: {'a_first_key': 'value', 'z_last_key': 'value', 'key_number_2': 'value', 'first_ [truncated]... != {'a_first_key': 'value', 'z_last_key': 'value', 'key_number_2': 'value', 'second [truncated]...
      {'a_first_key': 'value',
    -  'first_not_second': 'value',
       'key_number_2': 'value',
    +  'second_not_first': 'value',
       'z_last_key': 'value'}
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    FAILED (failures=1)
    
    0 讨论(0)
提交回复
热议问题