我是unittest的新手,仍然了解事情的运作方式。如果我有一个词典列表......例如:
mylist = [{"y": "xval", "v": "x1val"},
{"y": "yval", "v": "y1val"},
{"y": "zval", "v": "z1val"}]
当“y”=“yval”时,我将执行什么类型的断言/测试来验证“v”的值是“y1val”?也可能是字典y:yval在列表中不存在。
答案 0 :(得分:0)
没有可以直接测试的单元测试断言,但你可以使用循环。
# This is inside a TestCase method
for d in mylist:
if d['y'] == 'yval':
self.assertEqual(d['v'], 'y1val')
这可以概括为将每个'y'
条目与允许的'v'
条目相关联。
expected= {
'xval': 'x1val',
'yval': 'y1val',
'zval': 'z1val'
}
# This is in a TestCase method
for d in mylist:
self.assertEqual(d['v'], expected[d['y']])
答案 1 :(得分:0)
您可以尝试一些架构验证模块来验证您的结构:
>>>from voluptuous import Schema
>>>Schema([{'y':str, 'v':str}])
>>> s([{"y": "xval", "v": "x1val"},
{"y": "yval", "v": "y1val"},
{"y": "zval", "v": "z1val"}])
[{'y': 'xval', 'v': 'x1val'}, {'y': 'yval', 'v': 'y1val'}, {'y': 'zval', 'v': 'z1val'}]
要安装此模块,您可以使用:pip install voluptuous