2016-12-03 79 views
2

assertEqual(actual, expected)還是assertEqual(expected, actual)unittest.TestCase.assertEqual的參數順序

一方面,我看到很多使用assertEqual(actual, expected)的代碼。這包括examples in the unittest docsexamples in the Django docs

然而,這個測試assertEqual('foo', 'bar')是給我的輸出

- foo 
+ bar 

這恰好是一樣的PHPUnit的測試與assertEquals('foo', 'bar');

-'foo' 
+'bar' 

並且PHPUnit會具有$expected作爲第一個參數,然後通過$actual。這個差異也是我期望的expected, actual

那麼所有這些Python代碼我看到做錯了嗎?

我檢查了單元測試方法的定義,雖然這有極其有用的first, second參數名稱。

回答

1

作爲每assertEqual文件:

測試該第一和第二相等。如果這些值不相等,則測試將失敗。

所以,你可以把預期和實際放在任何地方,它會返回相同的結果。但通常使用實際結果作爲第一個參數,而預期結果作爲第二個參數。這也在Python 3's Unittest Example中證明:

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