最佳实践:断言方法的参数定位
Best practice: assert methods' argument positioning
来自文档
assertEqual(first, second, msg=None)
Test that first and second are equal. If the values do not compare equal, the test will fail.
对于first
和second
,期望值和实际值应该是多少?
assertEqual
本身并不关心:
======================================================================
FAIL: test_foo (foo.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/private/tmp/foo.py", line 6, in test_foo
self.assertEqual("actual string", "expected string")
AssertionError: 'actual string' != 'expected string'
- actual string
+ expected string
即使答案和评论暗示不同,也有实际的最佳实践:保持一致!。
因此,选择一个顺序(例如,您的 hard-coded/fixture 值排在第一位)并在其余代码中坚持该顺序。
来自文档
assertEqual(first, second, msg=None) Test that first and second are equal. If the values do not compare equal, the test will fail.
对于first
和second
,期望值和实际值应该是多少?
assertEqual
本身并不关心:
======================================================================
FAIL: test_foo (foo.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/private/tmp/foo.py", line 6, in test_foo
self.assertEqual("actual string", "expected string")
AssertionError: 'actual string' != 'expected string'
- actual string
+ expected string
即使答案和评论暗示不同,也有实际的最佳实践:保持一致!。
因此,选择一个顺序(例如,您的 hard-coded/fixture 值排在第一位)并在其余代码中坚持该顺序。