如果 json 括号不为空,如何使 api 调用测试失败

问题描述 投票:0回答:1

我有一个 Pytest 脚本进行 HTTP 调用:

def test_bid_submission_send():
# Act:
response = post_requests(token, '/xxx/api/xxx/bidsubmissions/send',
                         setpayload_bidsubmissions_send(powerplantId))
# Assert:
assert not response.json()["error"]
assert response.status_code == 200  # Validation of status code

回复是

{"success":[],"error":[{"hpsId":10037,"powerPlant":{"name":"Saurdal","id":16606,"regionId":20,"priceArea":2,"timeSeries":null,"units":[]}}]}

我想考试失败,如果

"error":[]

有内容(在我上面的例子中就有)

如果

"success":[]

有我想通过测试的内容

python json pytest
1个回答
0
投票

我想考试失败,如果

“错误”:[...]

j = response.json()
assert len(j["error"]) == 0

如果

“成功”:[] 有内容我想通过测试

assert len(j["success"]) != 0
© www.soinside.com 2019 - 2024. All rights reserved.