Python unittest - asserting dictionary with lists

ぃ、小莉子 提交于 2019-11-30 03:02:16

问题


While writing some tests for my class, I encountered interesting simple problem. I would like to assertDictEqual two dictionaries containing some list. But this lists may not be sorted in a same way -> which results in failed test

Example:

def test_myobject_export_into_dictionary(self):
    obj = MyObject()
    resulting_dictionary = {
            'state': 2347,
            'neighbours': [1,2,3]
        }
    self.assertDictEqual(resulting_dictionary, obj.exportToDict())

This fail from time to time, depending on order of elements in list

FAIL: test_myobject_export_into_dictionary
------------------------------------
-  'neighbours': [1,2,3],
+  'neighbours': [1,3,2],

Any ideas how to assert this in a simple way?

I was thinking about using set instead of list or sorting lists before comparison.


回答1:


You might try PyHamcrest (Example corrected)

assert_that(obj.exportToDict(), has_entries(
                                    { 'state': 2347,
                                      'neighbours': contains_inanyorder(1,2,3) }))

(The first value 2347 actually gets wrapped in an implicit equal_to matcher.)




回答2:


You can do:

a = {i:sorted(j) if isinstance(j, list) else j for i,j in resulting_dictionary.iteritems()}
b = {i:sorted(j) if isinstance(j, list) else j for i,j in obj.exportToDict().iteritems()}
self.assertDictEqual(a, b)



回答3:


How about using all:

assert all( (k,v) in resulting_dictionary.iteritems() 
            for (k,v) in obj.exportToDict().iteritems() )

I use something like this with py.test, but I think it should work for you.


A commenter pointed out that the order will screw me here---fair enough...I'd just use sets, then.




回答4:


maybe you can check for the two elements separately:

    obj_dict = obj.exportToDict()
    self.assertEqual(resulting_dictionary['state'], obj_dict['state'])
    self.assertCountEqual(resulting_dictionary['neighbours'], obj_dict['neighbours'])


来源:https://stackoverflow.com/questions/14491164/python-unittest-asserting-dictionary-with-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!