json path、xml、xpath、hamcrest断⾔体系

在做接口自动化测试的时候,有时候会遇到复杂的断言。

json path断言:

有时候要对嵌套类型的字典,要断言是否包含某字段,若没有借用其他断言系统,我们将会写成:

for item in r.json():
    if item['name']=='公众号':
        break

python有个json path的断言,这个断言在python界没有形成同一的语法,json path的语法这里就不作介绍了,json path可以解决断言嵌套字典是否包含某个字段。

assert jsonpath(r.json(), '$..name')[0] == '公众号'

xml断言:

因为目前的接口绝大部分都是json格式的返回结果,所以,xml和xpath的断言基本没怎么使用了

from requests_xml import XMLSession
session = XMLSession()
r = session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')
r.xml.links

xpath断言:

from requests_xml import XMLSession
session = XMLSession()
r = session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')
r.xml.links
item = r.xml.xpath('//item', first=True)
print(item.text)

hamcrest断⾔:

可以参考:https://jsonschema.net/,⽣成schema⽂件根据需要添加⾃定义规则

from jsonschema import validate
def test_get_login_jsonschema(self):
url = "https://testerhome.com/api/v3/topics.json"
data = requests.get(url, params={'limit': '2'}).json()
schema = json.load(open("topic_schema.json"))
validate(data, schema=schema)

猜你喜欢

转载自blog.csdn.net/chuancheng_zeng/article/details/109128168
今日推荐