requests做接口的断言
(1)if断言

案例:
import requests #导入request库
class Cms(object):
def init(self):
pass
def dl(self):
url1="http://49.233.201.254:8080/cms/manage/loginJump.do" # 接口的url
data1={
"userAccount":"admin",
"loginPwd":123456
} #接口的请求参数
headers1={"Content-Type":"application/x-www-form-urlencoded"} #接口请求类型
dx1=requests.post(url=url1,data=data1,headers=headers1) #接口请求方法requests.post
print(dx1.text) #{"code":"200","msg":"登录成功!","model":{}} #打印响应
wb=dx1.json()
if wb["msg"]=="登录成功!":
print("ok")
else:
print("no")
self.c=str(dx1.cookies).split(" ")[1]
print(self.c)
def cxyh(self):
url2="http://49.233.201.254:8080/cms/manage/queryUserList.do"
data2={
"startCreateDate":"","endCreateDate":"",
"searchValue":"","page":1
}
headers2={"Content-Type":"application/x-www-form-urlencoded",
"cookie":self.c}
dx2=requests.post(url=url2,data=data2,headers=headers2)
print(dx2.text)
if name == 'main':
d=Cms()
d.dl()
d.cxyh()
(2)assert断言
断言成功,就继续执行,断言失败就抛错误

案例:
import requests #导入request库
class Cms(object):
def init(self):
pass
def dl(self):
url1="http://49.233.201.254:8080/cms/manage/loginJump.do" # 接口的url
data1={
"userAccount":"admin",
"loginPwd":123456
} #接口的请求参数
headers1={"Content-Type":"application/x-www-form-urlencoded"} #接口请求类型
dx1=requests.post(url=url1,data=data1,headers=headers1) #接口请求方法requests.post
print(dx1.text) #{"code":"200","msg":"登录成功!","model":{}} #打印响应
wb=dx1.json()
assert wb["msg"]=="登录成功!"
self.c=str(dx1.cookies).split(" ")[1]
print(self.c)
def cxyh(self):
url2="http://49.233.201.254:8080/cms/manage/queryUserList.do"
data2={
"startCreateDate":"","endCreateDate":"",
"searchValue":"","page":1
}
headers2={"Content-Type":"application/x-www-form-urlencoded",
"cookie":self.c}
dx2=requests.post(url=url2,data=data2,headers=headers2)
wb2=dx2.json()
assert wb2["msg"] == "登录成功!"
if name == 'main':
d=Cms()
d.dl()
d.cxyh()
浙公网安备 33010602011771号