I could not find an unequivocal answer to the following simple question: how should the arguments be placed in assertEqual ? First, the expected result, then the actual or vice versa? Is there a generally accepted well-established practice in the Python community?

The official documentation and the standard library definitely do not define this moment. In the assertEqual itself assertEqual arguments are simply first and second .

assertEqual(actual, expected)

  • It looks somewhat more intuitive in my opinion: "this function call should return this value", which is a plus
  • Expressions of different lengths can be placed in place of the actual results - as a result, the eyes do not immediately stumble upon the expected value, which is minus

assertEqual(expected, actual)

  • somewhat more popular option in other languages, as far as I can tell
  • the expected value is immediately apparent

    1 answer 1

    In general, from a technical point of view, there is no difference, obviously. In fact, if you look at the source code, then at the end it turns out that the comparison is simply through == . Dockstreaming to TestCase.assertEqual :

    Fail if the two objects are unequal as determined by the '==' operator.

    In practice, the variant assertEqual(actual, expected) more common. In the documentation for various libraries / frameworks, it is usually in this version, in articles and on SO as well. As you said, it is intuitive, which is probably why.

    • one
      I consider it important to note that if __eq__ suddenly redefined and there is some kind of bug in it, then something can fall from changing the places of comparable ones - andreymal