前言
基于requests调用接口,所有内容都在一个py文件中,不利于代码维护,主要表现为:
1.公共部分(登陆)和测试用例在一个py文件
2.数据和测试用例在一个py文件
为了简化代码,考虑做两次封装,初步封装是将代码分层,然后将数据抽出来放到yaml
初步封装
1.登陆单独放test_util.py文件,在公共文件夹common下;
2.requests的四种get post delete patch请求类型封装成baseapi.py, 统一通过函数send调用,在公共文件夹common下;
3.被测系统的基本增删改查接口放wework.py, 在公共文件夹common下;
4.测试用例放到test_wework.py,在case文件夹下;
被测系统是企业微信https://developer.work.weixin.qq.com/document/path/91039
登陆
test_util.py
import requests
class TestUtil:
def test_get_token(self):
request_params = {
"corpid":"wwc8590b43a405366e",
"corpsecret":"S4FH2HT79chV315G0c6Ixso8VgJD5OG-qKPSCDehb9k"
}
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
r = requests.get(url, params=request_params)
return r.json()['access_token']
wework.py中调用test_get_token函数,from common.test_util import TestUtil
self.token = TestUtil().test_get_token()
class Wework(BaseApi):
def __init__(self):
self.token = TestUtil().test_get_token()
baseapi
查看request请求的源码,不管是哪种类型的请求,都是调用request函数,将request的四种请求封装成send函数,简化代码。
封装如下
baseapi.py
import requests
import json
class BaseApi:
params = {}
def send(self,data):
return requests.request(**data).json()
注意data是**data,**作用是解字典。
用send方法改造登陆接口
test_util.py
from common.baseapi import BaseApi
class TestUtil:
def test_get_token(self):
data = {
"method": "get",
"url": "https://qyapi.weixin.qq.com/cgi-bin/gettoken",
"params": {
"corpid": "wwc8590b43a405366e",
"corpsecret": "S4FH2HT79chV315G0c6Ixso8VgJD5OG-qKPSCDehb9k"
}
}
return BaseApi().send(data)['access_token']
首先导入BaseApi类,将数据都整合到data,data中包含method、url、get请求参数用params,corpid和corpsecrets看企业微信文档。请求成功,获取access_token。
增删改查
wework.py
import yaml
from common.test_util import TestUtil
from common.baseapi import BaseApi
class Wework(BaseApi):
def __init__(self):
self.token = TestUtil().test_get_token()
def test_post_list_id(self,cursor):
data = {
"method":"post",
"url":f"https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token={self.token}",
"json": {
"cursor": "",
"limit": 10000}
}
return self.send(data)
导入TestUtil和BaseApi,首先获取access_token。定义一个初始化函数,初始化传入的值赋给对象,即self.属性名,就可以被其他方法调用。
数据放配置文件
将接口数据data抽出来,放到配置文件wework.yaml, 首先在conmmon文件夹下创建wework.yaml
test_post_list_id:
"method": "post"
"url": "https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token=${token}"
"json":
"mobile": ${cursor}
"limit": 10000
test_post_getuserid:
"method": "post"
"url": "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=${token}"
"json":
"mobile": ${mobile}
改造baseapi.py
import requests
import json
class BaseApi:
params = {}
def send(self,data):
#字典转成字符串[dict→str]
raw_data = json.dumps(data)
#遍历替换value
for key,value in self.params.items():
raw_data= raw_data.replace("${"+key+"}",value)
# 字符串转为字典[str→dict]
data = json.loads(raw_data)
# **data 是关键字参数,接收的是一个dict,解字典
return requests.request(**data).json()
改造wework.py
import yaml
from common.test_util import TestUtil
from common.baseapi import BaseApi
class Wework(BaseApi):
def __init__(self):
self.token = TestUtil().test_get_token()
self.params["token"] = self.token
with open("../common/wework.yaml", encoding="utf-8") as f:
self.data = yaml.safe_load(f)
def test_post_list_id(self,cursor):
# data = {
# "method":"post",
# "url":f"https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token={self.token}",
# "json": {
# "cursor": "",
# "limit": 10000}
# }
self.params["cursor"] = cursor
return self.send(self.data["test_post_list_id"])
def test_post_getuserid(self, mobile):
# data = {
# "method":"post",
# "url":f"https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token={self.token}",
# "json": {
# "mobile": mobile}
# }
self.params["mobile"] = mobile
return self.send(self.data["test_post_getuserid"])
改造完成后,创建测试用例test_wework.py
from common.wework import Wework
# pytest --html=./../report/report.html --self-contained-html
#直接执行生成报告
class TestWework:
def test_post_list_id(self):
print(Wework().test_post_list_id(""))
def test_post_getuserid(self):
print(Wework().test_post_getuserid("13126966988"))
运行结果如下