APIResponseAssertions 类提供了断言方法,可用于在测试中对 APIResponse 进行断言
to_be_ok
确保响应状态代码在范围内。200..299
import re
from playwright.sync_api import expect
# ...
expect(response).to_be_ok()
not_to_be_ok
expect(response).not_to_be_ok()
可以看到,关于响应结果的断言,提供的方法还是比较少的。
实际使用过程中,我们可以先获取到响应结果的信息,再进行断言。
断言状态码
assert response.status == 200
断言响应头
assert response.headers["content-type"] == "application/json; charset=utf-8"
断言响应体json值
assert response.json()["name"] == "foobar"
断言响应体
assert response.body() == '{"status": "ok"}'