文章目录
接口测试概要
- 依据接口规范,写出测试用例,
- 使用软件工具,直接通过消息接口 对 被测系统 进行消息收发
- 验证被测系统行为是否正确。
- 接口测试,通常是对
服务端
做的 比较多,但是也有对客户端
做的。关键是看 被测系统是 服务端还是客户端。- HTTP协议的特点是,客户端发出一个HTTP请求给 服务端,服务端就返回一个HTTP响应。好像程序的API调用。所以 接口测试 通常又被称之为 API接口测试 或者 WEB API接口测试
安装Requests库
pip3 install requests
构建http请求
urlencoded格式
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=python
?后面的都是url的参数,每个参数之间是用 & 隔开的;
# 一般写法:
response = requests.get('https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=python')
# 如果url参数中包含特殊字符,如:&符号时就不能使用上面的写法了,这样的话会被拆分两个参数
# 可以把这些参数放到一个字典里面,然后把字典对象传递给 Requests请求方法的 params 参数,如下
urlpara = {
'ie':'utf-8',
'rsv_bp':'1',
'tn':'baidu',
'wd':'py&thon'
}
response = requests.get('https://www.baidu.com/s',params=urlpara)
json消息体格式
json 是一种表示数据的语法格式,使用python中json库的dumps方法:
import requests,json
payload = {
"data":"数据",
"Progress":"30%",
"describe":[
{
"No" : 1,
"desc": "问题1...."
},
{
"No" : 2,
"desc": "问题2...."
}
]
}
response = requests.post("https://www.baidu.com/", data=json.dumps(payload))
# 或者直接将数据对象专递给json
response = requests.post("https://www.baidu.com/", json=payload)
xml消息体格式
payload = '''
<?xml version="1.0" encoding="UTF-8"?>
<WorkReport>
<p>良好</p>
<h1>30%</h1>
</WorkReport>
'''
r = requests.post("https://www.baidu.com/",
data=payload.encode('utf8'))
print(r.text)
检查http响应
查看状态码
import requests
response = requests.get('https://www.baidu.com/')
print(response.status_code)
结果:200
# 可以在url中传入错误的url,就会返回404或其它的状态码
查看消息头
import requests, pprint
# pprint是格式化打印,可以直观的打印出json格式
# proxies 是因为我本地设置了代理
proxies = {
'http': 'http://127.0.0.1:8081',
'https': 'http://127.0.0.1:8081',
}
response = requests.get('https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=python',
proxies=proxies)
print(type(response.headers))
pprint.pprint(dict(response.headers))
结果:
<class 'requests.structures.CaseInsensitiveDict'>
{'Accept-Ranges': 'bytes',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Length': '227',
'Content-Type': 'text/html',
'Date': 'Sat, 07 Nov 2020 09:22:10 GMT',
'P3p': 'CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM '
'"',
'Pragma': 'no-cache',
'Server': 'BWS/1.1',
'Set-Cookie': 'BD_NOT_HTTPS=1; path=/; Max-Age=300, '
'BIDUPSID=9CA7490565DADD39C7FF81BB8B4DFD00; expires=Thu, '
'31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; '
'domain=.baidu.com, PSTM=1604740930; expires=Thu, 31-Dec-37 '
'23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, '
'BAIDUID=9CA7490565DADD396A1C01457310822F:FG=1; '
'max-age=31536000; expires=Sun, 07-Nov-21 09:22:10 GMT; '
'domain=.baidu.com; path=/; version=1; comment=bd',
'Strict-Transport-Security': 'max-age=0',
'Traceid': '1604740930068017767416466434451384432341',
'X-Ua-Compatible': 'IE=Edge,chrome=1'}
查看消息体
import requests
response = requests.get('https://www.baidu.com/')
print(response.text)
# 如果我们要直接获取消息体中的字节串内容,可以使用 content 属性,可以直接指定编码格式
print(response.content.decode('utf8'))
requests处理session-cookie
import requests
# 打印HTTP响应消息的函数
def printResponse(response):
print('\n\n-------- HTTP response * begin -------')
# 打印状态码
print(response.status_code)
# 循环取出响应消息头信息
for k, v in response.headers.items():
print(f'{k}: {v}')
# 打印消息体内容
print(response.content.decode('utf8'))
print('-------- HTTP response * end -------\n\n')
# 创建 Session 对象
s = requests.Session()
# 通过 Session 对象 发送请求
response = s.post("http://127.0.0.1/login",
data={
'username': 'xxxx',
'password': 'xxxx'
})
printResponse(response)
# 通过 Session 对象 发送请求
response = s.get("http://127.0.0.1/list_customer",
params={
'pagesize': xx,
'pagenum': xx,
'keywords': '',
})
printResponse(response)