一、目前市面上实现接口自动化测试的方式和行情
- 主流接口测试工具实现接口自动化(适用于中小型的公司或项目)10-15K左右,50%Postman+Newman+git/Svn+Jenkins(基于Javascript语言)接口自动化
Jmeter+Ant+Git/Svn+Jenkins (基于Java和BeanShell语言)接口自动化 - 基于代码的接口自动化(适用于大中型公司或一线互联网公司)15-30K左右,40%Python+Requests+Yaml+Pytest+Allure+Logging+热加载+Jenkins持续集成接口自动化
- 基于平台的接口自动化(适用于于特大型或外包型公司)30K以上,10%
测试开发。
二、全面认识Requests模块以及常用的方法和底层原理requests模块式用于发送Http请求以及接口http响应的python的第三方库。
安装
pip install requests
详解requests: Alt+Enter自动导包
三、接口自动化实战和封装
四、接口自动化框架封装(统一请求封装)
data一般填的是表单,表单就是键值对格式
下面这种传的是json型数据
简单理解:大括号的是对象,中括号的是数组。
json是字符串,字典是对象。
r"E:\picture"=“E:\picture”
test_api.py
import pytest
import requests
# requests.get()
# requests.post()
# requests.put()
# requests.delete()
# requests.request()
# requests.session()
class TestApi:
access_token = "" # 类变量,是在所有方法中都可以使用的变量,通过类名来访问
# 获取鉴权码
def test_get_token(self):
url = "https://api.weixin.qq.com/cgi-bin/token"
datas = {
"grant_type": "client_credential",
"appid": "wx74a8627810cfa308",
"secret": "e40a02f9a8097df497e6aaf93ab80"
}
res = requests.get(url=url, params=datas)
# 把json数据保存到result里边,再用result的key去取access_token值
# 取到之后,把值保存到全局变量access_token里边,然后将取到的值传到后面,实现接口关联。
result = res.json()
TestApi.access_token = result["access_token"]
print(res.json())
# 编辑标签接口
def test_edit_flag(self):
# access_token来自上面的res.json()的返回值
# 字符串的拼接
url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=" + TestApi.access_token
datas = {"tag": {"id": 134, "name": "广东人"}}
res = requests.post(url=url, json=datas)
print(res.json()) # 返回的是字典
# 文件上传 通过file来传参,不是通过data和json来传参
# post请求,所以知道要上传的是def post(url, data=None, json=None, **kwargs)括号里的值
def test_file_upload(self):
url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + TestApi.access_token
datas = {
"media": open("E:\\shu.jpg")
}
res = requests.post(url=url, files=datas)
print(res.json())
if __name__ == '__main__':
pytest.main()
接口统一请求封装
send_request.py
import requests
class SendRequest:
# 回话,回话对象能够自动的管理Cookie关联
# 类变量通过类名调用,即要用sess时,要用SendRequest.sess来访问
sess = requests.Session()
# all_send_request是一个方法,方法通过对象来访问,
# 即要调用all_send_request得用SendRequest().all_send_request
def all_send_request(self, method, url, **kwargs):
print("-----接口测试开始-----")
print("请求方式:%s" % method)
print("接口地址:%s" % url)
if "params" in kwargs.keys():
print("请求params参数:%s" % kwargs["params"])
if "json" in kwargs.keys():
print("请求json参数:%s" % kwargs["json"])
if "files" in kwargs.keys():
print("请求files参数:%s" % kwargs["files"])
res = SendRequest.sess.request(method, url, **kwargs)
print("接口返回:%s"%res.text)
print("-----接口测试结束-----")
print("\n")
return res
test_api.py
import pytest
import requests
from common.send_request import SendRequest
# requests.get()
# requests.post()
# requests.put()
# requests.delete()
# requests.request()
# requests.session()
class TestApi:
access_token = "" # 类变量,是在所有方法中都可以使用的变量,通过类名来访问
# 获取鉴权码
def test_get_token(self):
url = "https://api.weixin.qq.com/cgi-bin/token"
datas = {
"grant_type": "client_credential",
"appid": "wx74a8627810cfa308",
"secret": "e40a02f9a8097df497e6aaf93ab80"
}
res = SendRequest().all_send_request(method="get", url=url, params=datas)
# res = requests.get(url=url, params=datas)
# 把json数据保存到result里边,再用result的key去取access_token值
# 取到之后,把值保存到全局变量access_token里边,然后将取到的值传到后面,实现接口关联。
result = res.json()
TestApi.access_token = result["access_token"]
print(res.json())
# 编辑标签接口
def test_edit_flag(self):
# access_token来自上面的res.json()的返回值
# 字符串的拼接
url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=" + TestApi.access_token
datas = {"tag": {"id": 134, "name": "广东人"}}
res = SendRequest().all_send_request(method="post", url=url, json=datas)
# res = requests.post(url=url, json=datas)
print(res.json()) # 返回的是字典
# 文件上传 通过file来传参,不是通过data和json来传参
# post请求,所以知道要上传的是def post(url, data=None, json=None, **kwargs)括号里的值
def test_file_upload(self):
url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + TestApi.access_token
datas = {
"media": open("E:\\shu.jpg")
}
res = SendRequest().all_send_request(method="post", url=url, files=datas)
# res = requests.post(url=url, files=datas)
print(res.json())
if __name__ == '__main__':
pytest.main()