比较python nosetests中的2个词典

时间:2018-01-25 16:15:52

标签: python nose

我使用python nosetests编写我的单元测试并且需要比较2个词典 -

这是我的鼻子测试方法,需要知道比较dicts的正确方法。

def test_outputs_processed(self):
        p = Processing(test_data)
        self.result = [
          {'y2': 1423, 'x2': 923,'y1': 1286, 'x1': 277},
          {'y2': 1345, 'x2': 1953,'y1': 1053, 'x1': 1639},
          {'y2': 1651, 'x2': 923, 'y1': 1286, 'x1': 277},
          {'y2': 1913, 'x2': 850, 'y1': 1570, 'x1': 511,}
          ]
        self.assertEqual(p.execute(), self.result)

1 个答案:

答案 0 :(得分:0)

Here's what I've used in the past. It first checks to see of the set of keys is equal then checks the values, key for key:

def equal_dicts(x, y):

    keys_match = set(x.keys()) == set(y.keys())
    vals_match = all([x[k] == y[k] for k in x])

    return keys_match and vals_match