python自动化学习六:断言
使用mock工具,模拟了3个接口调用,可以随意编写接口的响应结果。
[{"description": "登录01,第一个模拟的接口,请求Content-Type:application/json","request": {"uri": "/login","method": "post","headers": {"Content-Type": "application/json"},"json": {"username": "admin","password": "123456"}},"response": {"status": 200,"headers": {"Content-Type": "application/json;charset=UTF-8"},"json": {"code": "10000","message": "登录成功","data": {"userid": "3882","name": "lucy","addr": "chengdu","tel": "13355556666","token": "mocktoken9gd878ad"}}}},{"description": "post请求,模拟的第二个接口,订单详情页,请求Content-Type:application/x-www-form-urlencoded","request": {"uri": "/order","method": "post","headers": {"Content-Type": "application/x-www-form-urlencoded"},"forms": {"ordernum": "20201010","userid": "3218"}},"response": {"status": 200,"headers": {"Content-Type": "application/json;charset=UTF-8"},"json": {"orderNo": "LO20111216292785711001","productId": 2,"skuId": 22,"expert": 0,"count": 1,"payType": "WECHAT_PAY","payClient": "MINI","mobile": "15812345678","goodname": "apple","orderType": "NORMAL","logistics": "NONE"}}},{"description": "商品详情页,模拟的第3个接口,请求Content-Type:multipart/form-data","request": {"uri": "/details","method": "post","forms": {"goodid": "1","date": "20220101"}},"response": {"status": 200,"headers": {"Content-Type": "application/json;charset=UTF-8"},"json": {"goodid": "1","name": "pears","status": "1","stock": "25","vip_amount ": "24","sale_amount": "18","market_amount": "23.6","attribute_value": "丰富营养","product_attr_vals": "单果300g","share_title": "分享标题","share_descrip": "分享描述","license": "营业执照","employType": 0,"other_fruits":{"peach":5,"origence":8,"tomoto":90},"computer_type":{"aaa":[1,3,5],"name":["rose","jime8888"]}}}}
]//headers中的"Content-Type": "application/json"指传递参数以json方式//headers中的"token": "mocktoken"指请求头中必须加上token才能访问//status:定义http响应状态码
用postman进行接口调用,查看返回结果是不是正确的。
想着如何对响应结果中的computer_type的第二个字段name的第二个值进行断言,判断其是不是jime8888呢?
下面是我写的python代码,验证通过是ok的。
import requestsurl="http://127.0.0.1:9091/details"
data={"goodid":"1","date":"20220101"}
resp=requests.post(url,data=data)
print(resp.json())# 设置断言
assert 200==resp.status_code
assert resp.json()["other_fruits"]["peach"]==5
# assert resp.json()["computer_type"]["name"][1]=='jime'
assert resp.json()["computer_type"]["name"][1]=='jime',f"断言不通过,computer_type的name属性的第二个值不是jime,实际是{resp.json()["computer_type"]["name"][1]}"