简单接口测试_登录
接口测试需要用到的库为Python第三方库requests
首先,需要安装
pip install requests
其次,使用requests
import requests
记录实际用法(登录)
第一版:
import requests
url = "登录地址"
header ={
"Content-type":"application/json;charset=UTF-8"
}
params = {"username": "账号", "password": "密码", "isAutoLogin": "false"}
response = requests.post(url,headers=header,json=params,verify=False)
r = response.json()
print(r)
第二版:
# 接口调用异常处理,增加try,except处理,对于返回response.status_code,返回200进行结果比对,不是200数据异常信息
import requests
url = "登录地址"
header ={
"Content-type":"application/json;charset=UTF-8"
}
params = {"username": "账号", "password": "密码", "isAutoLogin": "false"}
try:
response = requests.post(url, headers=header, json=params, verify=False )
if response.status_code == 200:
r = response.json()
print("sucess")
else:
raise Exception("http error info:%s" % response.status_code)
except Exception as e:
raise e
第三版:
class GcbLogin: # 封装类和方法
def login(self,username,password):
url = "登录地址"
header = {
"Content-type": "application/json;charset=UTF-8"
}
params = {"username": username, "password": password, "isAutoLogin": "false"}
response = requests.post(url,headers=header,json=params,verify=False)
r = response.json()
# 拼接cookie
cookie = "LOGIN_USERNAME=" + params['username'] + ";LOGIN_SEQUENCE=" + r['body']['employee']['loginSequence']
return cookie
t = GcbLogin().login("账号","密码")
print(t)