一、接口文档怎么看?
http://www.aaa.com/api.php?s=index/index\&application=app\&application_client_type=weixi
n&token=tokenvalue&ajax=ajax
参数解释:
- http 协议
- www.aaa.com IP和端口
- api.php 接口的地址
- s=index/index 接口名称以 控制器/方法 组合、如 index/index
- application=app 公共参数,取值:web或app
- application_client_type=weixin 公共参数
- token=token 公共参数
- ajax=ajax 异步请求
能够请求成功的URL应该是这样的:
http:// 101.34.221.219:8010/ api.php?
s=index/index&application=app&application_client_type=h5&token=token&ajax=ajax
nginx:80
tomcat:8080
二、接口自动化实战
看上一篇笔记写的自动化实战项目
可以发现一些问题:
1.发现有很多重复的冗余的代码
2.多个py文件之间的接口是不能自动化的关联cookie
接口自动化测试框架封装技术点:统一请求封装
目的:
1.去重重复的冗余的代码
2.跨py文件实现通过一个sess来自动关联有cookie关联的接口。
3.设置统一的公共参数,统一的文件处理,统一的异常处理,统一的日志监控,统一的用例校验等
等。。。
实现步骤:
1.在commons目录下创建封装一个同意请求的类
2.改造之前写的接口请求代码,
将TestShopxo.sess.request()用封装的方法替换RequestUtil().sed_all_request()【红色框】
3.将公共参数在RequestUtil().sed_all_request()提供【蓝色框】,将接口自动化脚本中的重复代码删除【黄色框】
python
import requests
#统一请求封装
class RequestUtil:
sess = requests.session()
#使用同一个session会话的模块
def sed_all_request(self,**kwargs):
total_params = {
"application": "app",
"application_client_type": "h5"
}
for key,value in kwargs.items():
if key =="params":
kwargs["params"].update(total_params)
elif key =="files":
for file_key,file_value in value.items():
value[file_key] = open(file_value,"rb")
#发送请求
res = RequestUtil.sess.request(**kwargs)
return res
python
import jsonpath
import requests
from commons.request_util import RequestUtil
from commons.yaml_util import write_yaml, read_yaml
class TestShopxo:
# token = ""
# sess = requests.session()
#首页列表接口
def test_start_list(self):
method = "post"
url ="http://shop-xo.hctestedu.com/index.php"
parmas = {
# "application" : "app",
# "application_client_type" : "h5",
"s" : "api/index/index"
}
res = RequestUtil().sed_all_request(method=method, url=url, params=parmas)
print(res.json())
# 登陆接口
def test_login_shopxo(self):
method = "post"
url ="http://shop-xo.hctestedu.com/index.php"
params = {
# "application": "app",
# "application_client_type": "h5",
"s": "api/user/login"
}
json = {
"accounts": "yut2001",
"pwd": "123456",
"verify": "rib5",
"type": "username"
}
res = RequestUtil().sed_all_request(method=method, url=url, params=params, json=json)
print(res.json())
# 提取token
TestShopxo.token = jsonpath.jsonpath(res.json(), "$.data.token")[0]
data = {"token":jsonpath.jsonpath(res.json(), "$.data.token")[0]}
write_yaml(data)
def test_order_list(self):
method = "post"
url = "http://shop-xo.hctestedu.com/index.php"
params = {
# "application": "app",
# "application_client_type": "h5",
"s": "api/order/index",
"token": read_yaml("token")
}
json = {
"page": "1",
"keywords": "",
"status": "-1",
"is_more": "1"
}
res = RequestUtil().sed_all_request(method=method, url=url, params=params, json=json)
print(res.json())
#商品详情页
def test_order_detail(self):
method = "post"
url ="http://shop-xo.hctestedu.com/index.php"
params = {
# "application": "app",
# "application_client_type": "h5",
"s": "api/goods/detail"
}
json = {
"goods_id": "12"
}
res = RequestUtil().sed_all_request(method=method, url=url, params=params, json=json)
print(res.json())
五、接口自动化测试框架封装技术点:接口关联封装
目的:
1.统一管理接口关联的中间变量(最好是接口执行之后能够看到中间变量的值,利于拍错)
2.解决多个py文件中间的中间变量关联的问题
方案:
接口关联封装是通过一个yaml文件来保存中间变量,让后通过读,写以及清空来处理这些中间变量
所有用例请求之前去清空还是所有用例请求之后去清空?==== 之前
实现步骤:
1.创建一个 extract.yaml文件,用来临时存储中间变量
2.调整yaml工具类
3.创建 conftest.py文件实现对yaml工具类的调用