在为我的班级编写一些测试时,我遇到了有趣的简单问题。我想 assertDictEqual 包含一些列表的两个字典。但是这个列表可能不会以相同的方式排序 -> 导致测试失败
例子:
def test_myobject_export_into_dictionary(self):
obj = MyObject()
resulting_dictionary = {
'state': 2347,
'neighbours': [1,2,3]
}
self.assertDictEqual(resulting_dictionary, obj.exportToDict())
这有时会失败,具体取决于列表中元素的顺序
FAIL: test_myobject_export_into_dictionary
------------------------------------
- 'neighbours': [1,2,3],
+ 'neighbours': [1,3,2],
任何想法如何以简单的方式断言这一点?
在比较之前,我正在考虑使用set
代替list
或排序列表。