前言
在上一期《当你接触到了Requests类库,你就已经掌握了Python接口自动化测试框架的终极奥义》中介绍了Requests的终极奥义。这期将进行终极奥义-Requests的实战示例。
使用到的工具
-
编程语言: Python 3.8
-
编译器: Pycharm
-
Python类: Requests
1. 基础用法requests.post
以比较常见的POST类型接口为例,进行标准的接口请求。
import json
import requests
# 系统域名
sys_host = 'https://******.******.com'
# 接口路径
api_path = '/api/users/login'
# 接口请求头
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'accept-language': 'zh',
'Connection': 'keep-alive',
'Content-Length': '62',
'Content-Type': 'application/json; charset=utf-8',
'Host': '******.******.com',
'Origin': 'https://******.******.com',
'sec-ch-ua': '"Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'token': '',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62'
}
# POST请求报文
request = {
"username": "******.******@******.******",
"password": "************"
}
# 执行接口请求,接受返回信息
response = requests.post(url=sys_host + api_path, data=json.dumps(request), headers=headers)
print('response: %s' % response)
# 将返回信息处理成json格式
response_json = response.json()
print('response_json(单引号): %s' % response_json)
# 将返回信息处理成双引号的json格式(方便数据格式化操作)
response_json_2 = json.dumps(response_json)
print('response_json(双引号): %s' % response_json_2)
# 将返回信息处理成双引号的json格式(处理中文编码格式)
response_json_3 = json.dumps(response_json, ensure_ascii=False)
print('response_json(双引号)(中文格式处理): %s' % response_json_3)
控制台返回信息
2. 处理接口返回信息response.status_code
import json
import requests
# 系统域名
sys_host = 'https://******.******.com'
# 接口路径
api_path = '/api/users/login'
# 接口请求头
headers = {
# 'Accept': '*/*',
# 'Accept-Encoding': 'gzip, deflate, br',
'accept-language': 'zh',
# 'Connection': 'keep-alive',
# 'Content-Length': '62',
'Content-Type': 'application/json; charset=utf-8',
# 'Host': '******.******.com',
# 'Origin': 'https://******.******.com',
# 'sec-ch-ua': '"Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
# 'sec-ch-ua-mobile': '?0',
# 'sec-ch-ua-platform': '"Windows"',
# 'Sec-Fetch-Dest': 'empty',
# 'Sec-Fetch-Mode': 'cors',
# 'Sec-Fetch-Site': 'same-origin',
# 'token': '',
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62'
}
# POST请求报文
request = {
"username": "******.******@******.******",
"password": "************"
}
# 执行接口请求,接受返回信息
response = requests.post(url=sys_host + api_path, data=json.dumps(request), headers=headers)
print('response: %s' % response)
# 接口返回状态码
print('status_code: %s' % response.status_code)
# 接口编码方式
print('encoding: %s' % response.encoding)
# 将返回信息处理成json格式
response_json = response.json()
print('response_json(单引号): %s' % response_json)
# 获取接口自定义的状态码值
code = response_json['code']
print('code: %s' % code)
# 获取接口自定义的信息
msg = response_json['msg']
print('msg: %s' % msg)
# 获取接口返回信息中token信息
sys_token = response_json['data']['token']
print('sys_token: %s' % sys_token)
控制台返回信息
在上面的示例中:
1)以POST类型接口为例,我们简化了headers信息,因为大多数接口在进行鉴权时,只需要提供关键的key:value
即可
2)通过response.status_code
接受http请求的返回状态,例如200则表示请求成功
3)通过response.encoding
接受http请求的相应内容编码方式
3)通过response_json[field_name1][field_name2]
接受http请求的相应节点下的字段值
3.字段值断言Assert
import json
import requests
# 系统域名
sys_host = 'https://******.******.com'
# 接口路径
api_path = '/api/users/login'
# 接口请求头
headers = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# POST请求报文
request = {
"username": "******.******@******.******",
"password": "************"
}
# 执行接口请求,接受返回信息
response = requests.post(url=sys_host + api_path, data=json.dumps(request), headers=headers)
print('response: %s' % response)
# 将返回信息处理成json格式
response_json = response.json()
print('response_json: %s' % response_json)
# 获取接口自定义的状态码值
code = response_json['code']
print('code: %s' % code)
# 获取接口自定义的信息
msg = response_json['msg']
print('msg: %s' % msg)
# 获取接口返回信息中token信息
sys_token = response_json['data']['token']
print('sys_token: %s' % sys_token)
# 接口字段值进行断言
assert code == 0
assert msg == '操作成功'
assert sys_token
assert sys_token == '1111'
控制台返回信息
常用断言方式
assert xx :判断 xx 为真
assert not xx :判断 xx 不为真
assert a in b :判断 b 包含 a
assert a == b :判断 a 等于 b
assert a != b :判断 a 不等于 b
4. GET类型接口requests.get()
,接口数据传递
import json
import requests
# 系统域名
sys_host = 'https://******.******.com'
"""
用户登录接口
"""
# 接口路径
api_path_login = '/api/users/login'
# 接口请求头
headers_login = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# 请求报文
headers_login = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# 执行接口请求,接受返回信息
response_login = requests.post(url=sys_host + api_path_login,
data=json.dumps(request_login),
headers=headers_login)
# 将返回信息处理成json格式
response_json_login = response_login.json()
print('response_json_login: %s' % response_json_login)
# 获取接口返回信息中token信息
sys_token = response_json_login['data']['token']
print('sys_token: %s' % sys_token)
"""
simple接口
"""
# 接口路径
api_path_simple = '/api/apps/simple'
# 接口请求头
headers_simple = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# 接口数据传递至请求头
headers_simple.setdefault('token', sys_token)
print('headers_simple: %s' % headers_simple)
# 执行接口请求,接受返回信息
response_simple = requests.get(url=sys_host + api_path_simple,
headers=headers_simple)
# 将返回信息处理成json格式
response_json_simple = response_simple.json()
print('response_json_simple: %s' % response_json_simple)
# 获取接口自定义的状态码值
code = response_json_simple['code']
print('code: %s' % code)
# 获取接口自定义的信息
msg = response_json_simple['msg']
print('msg: %s' % msg)
# 获取接口返回信息中对应字段信息
data = response_json_simple['data']
print('len(data): %d' % len(data))
# 接口字段值进行断言
assert code == 0
assert msg == '操作成功'
assert len(data) > 0
控制台返回信息
在上面的示例中:
1)requests.get(url=sys_host + api_path_simple,headers=headers_simple) 进行get类型接口的执行
2)headers_simple.setdefault('token', sys_token),将登录接口中的token传递到get接口请求头中
5. POST接口请求体中数据传递
import json
import requests
# 系统域名
sys_host = 'https://******.******.com'
"""
用户登录接口
"""
# 接口路径
api_path_login = '/api/users/login'
# 请求方式
method_login = 'post'
# 接口请求头
headers_login = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# 请求报文
request_login = {
"username": "******.******@******.******",
"password": "************"
}
# 执行接口请求,接受返回信息
response_login = requests.Session().request(method=method_login,
url=sys_host + api_path_login,
data=json.dumps(request_login),
headers=headers_login)
# 将返回信息处理成json格式
response_json_login = response_login.json()
print('response_json_login: %s' % response_json_login)
# 获取接口返回信息中token信息
sys_token = response_json_login['data']['token']
print('sys_token: %s' % sys_token)
"""
simple接口
"""
# 接口路径
api_path_simple = '/api/apps/simple'
# 请求方式
method_simple = 'get'
# 接口请求头
headers_simple = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# 接口数据传递至请求头
headers_simple.setdefault('token', sys_token)
print('headers_simple: %s' % headers_simple)
# 执行接口请求,接受返回信息
response_simple = requests.Session().request(method=method_simple,
url=sys_host + api_path_simple,
headers=headers_simple)
# 将返回信息处理成json格式
response_json_simple = response_simple.json()
print('response_json_simple: %s' % response_json_simple)
# 获取接口返回信息中对应字段值
code_simple = response_json_simple['code']
msg_simple = response_json_simple['msg']
data_simple = response_json_simple['data']
# 接口字段值进行断言
assert code_simple == 0
assert msg_simple == '操作成功'
assert len(data_simple) > 0
"""
verify接口
"""
# 接口路径
api_path_verify = '/api/users/verify'
# 请求方式
method_verify = 'post'
# 接口请求头
headers_verify = {
'accept-language': 'zh',
'Content-Type': 'application/json; charset=utf-8'
}
# 接口数据传递至请求头
headers_verify.setdefault('token', sys_token)
# 接口数据传递至请求报文
request_verify = {
"token": sys_token
}
# 执行接口请求,接受返回信息
response_verify = requests.Session().request(method=method_verify,
url=sys_host + api_path_verify,
data=json.dumps(request_verify),
headers=headers_verify)
# 将返回信息处理成json格式
response_json_verify = response_verify.json()
print('response_json_verify: %s' % response_json_verify)
# 获取接口返回信息中对应字段值
code_verify = response_json_simple['code']
msg_verify = response_json_simple['msg']
data_verify = response_json_simple['data']
# 接口字段值进行断言
assert code_verify == 0
assert msg_verify == '操作成功'
assert data_verify is not None
控制台返回信息
在上面的示例中:
1)login接口登录后,将接口返回信息中的token给sys_token
2)通过headers_simple.setdefault('token', sys_token),request_verify = {"token": sys_token},可以将登录接口中的token传递到get接口请求头中,或者传递给post接口的请求体中。
总结
常用的接口类型为post
、get
,通过获取接口返回信息中的关键字段信息,赋值给全局变量,方便其他接口在请求头/请求体中进行数据传递,针对返回信息,可以使用assert
进行字段值的断言。
网友评论