The xUnit testing convention is expected/actual. So, for many that is the natural order since that's what they learnt.
Interestingly in a break from convention for an xUnit framework qunit goes for actual/expected. At least with javascript you can just create a new function that encapsulates the old one and assign it the original variable:
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.
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, Assertequals 使用 assertDictequals in >=2.7 and >=3.1
就我个人而言,我更希望它被设计为 assertEqual(actual, expected),因为,考虑到 assert和 if之间的概念相似性,我希望它遵循 ABC3的标准,例如 if a == 1。
(附注: 的确有不同的意见,其中 提示按“反向顺序”写入 if 语句,即 if(1==a) {...},为了防止你意外地错过了一个 =。但即使在 C/C + + 世界中,这种风格也远非常规。而且,如果您正好在编写 Python 代码,那么首先就不容易出现这种令人讨厌的输入错误,因为 if a = 1在 Python 中是无效的。)
The practical convincing reason to do assertEqual(expect, actual), is that the unittest library in your language likely already follows that order to generate readable error message. For example, in Python, when you do assertEqual(expected_dictionary, actual_dictionary), unittest will display missing keys in actual with prefix ABC2, and extra keys with prefix +, just like when you do a git diff old_branch new_branch.
Intuitive or not, this is the single most convincing reason to stick with the assertEqual(expected, actual) order. If you happen to not like it, you better still accept it, because "practicality beats purity".
最后,如果您需要一种方法来帮助您记住顺序,这个答案将 assertEqual(expected_result, actual_calculation)与赋值语句顺序 result = calculate(...)进行比较。这可能是一个很好的方法来记忆事实上的行为,但恕我直言,这不是无可争议的推理,这个顺序是更直观的。