如何在字典列表的 Json 响应中断言值
How to assert values in a Json response of List of dictionaries
这是我从 GET 请求中得到的 Json 响应。
[
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 124,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "failed"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 215,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "success"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 746,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-02-04T11:46:32.465726+00:00",
"state": "pause"
}
]
从响应中,我如何断言每个词典列表都有 run_date、id、job_id 和 state.
如果得到 id == 215 并且其状态为成功,我可以断言吗?
提前致谢
EDIT: You can use some schema library like Marshmallow to assert if each run belongs to a certain schema
我会尝试这样的事情:
def assert_runs(runs, assertions, success_ids):
for run in runs:
for assertion in assertions:
assert assertion in run
if run['id'] in success_ids:
try:
assert run['state'] == 'success'
except AssertionError as e:
print('ERROR', run['id'], run['state'])
raise e
传递运行列表、断言和 id 以断言成功
但这太嵌套了,所以我会分成两个函数:
def assert_ids(run, success_ids):
if run['id'] in success_ids:
try:
assert run['state'] == 'success'
except AssertionError as e:
print('ERROR', run['id'], run['state'])
raise e
def assert_runs(runs, assertions, success_ids):
for run in runs:
for assertion in assertions:
assert assertion in run
assert_ids(run, success_ids)
并将其命名为:
assertions = ['run_date', 'id', 'job_id', 'state']
runs = [
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 124,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "failed"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 215,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "success"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 746,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-02-04T11:46:32.465726+00:00",
"state": "pause"
}
]
assert_runs(runs, assertions, [215, 746])
这是我从 GET 请求中得到的 Json 响应。
[
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 124,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "failed"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 215,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "success"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 746,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-02-04T11:46:32.465726+00:00",
"state": "pause"
}
]
从响应中,我如何断言每个词典列表都有 run_date、id、job_id 和 state.
如果得到 id == 215 并且其状态为成功,我可以断言吗?
提前致谢
EDIT: You can use some schema library like Marshmallow to assert if each run belongs to a certain schema
我会尝试这样的事情:
def assert_runs(runs, assertions, success_ids):
for run in runs:
for assertion in assertions:
assert assertion in run
if run['id'] in success_ids:
try:
assert run['state'] == 'success'
except AssertionError as e:
print('ERROR', run['id'], run['state'])
raise e
传递运行列表、断言和 id 以断言成功 但这太嵌套了,所以我会分成两个函数:
def assert_ids(run, success_ids):
if run['id'] in success_ids:
try:
assert run['state'] == 'success'
except AssertionError as e:
print('ERROR', run['id'], run['state'])
raise e
def assert_runs(runs, assertions, success_ids):
for run in runs:
for assertion in assertions:
assert assertion in run
assert_ids(run, success_ids)
并将其命名为:
assertions = ['run_date', 'id', 'job_id', 'state']
runs = [
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 124,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "failed"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 215,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-01-31T11:14:11.571934+00:00",
"state": "success"
},
{
"run_date": "2020-01-31T11:14:11+00:00",
"id": 746,
"job_id": "2020-01-31T11:14:11+00:00",
"start_date": "2020-02-04T11:46:32.465726+00:00",
"state": "pause"
}
]
assert_runs(runs, assertions, [215, 746])