Python3+Requests+Excel完整接⼝⾃动化测试框架的实
框架整体使⽤Python3+Requests+Excel:包含对实时token的获取
1、------base
-------runmethond.py
runmethond:对不同的请求⽅式进⾏封装
import json
import requests
requests.packages.urllib3.disable_warnings()
class RunMethod:
def post_main(self, url, data, header=None):
res = None
if header != None:
res = requests.post(url=url, data=data, headers=header,verify=False)
else:
res = requests.post(url=url, data=data,verify=False)
return res.json()
def get_main(self, url, data=None, header=None):
res = None
if header != None:
res = (url=url, params=data, headers=header, verify=False)
else:
res = (url=url, params=data, verify=False)
return res.json()
def run_main(self, method, url, data=None, header=None):
res = None
if method == 'Post':
res = self.post_main(url, data, header)
else:
res = _main(url, data, header)
return json.dumps(res, indent=2, sort_keys=True, ensure_ascii=False)
if __name__ == '__main__':
url = '/post'
data = {
'cart': '11'
}
run = RunMethod()
run_test = run.run_main(method="Post", url=url, data=data)
print(run_test)
2、------data
------data_config.py
data_config:获取excel模块中数据
class global_val:
Id = '0'
request_name = '1'
url = '2'
run = '3'
request_way = '4'
header = '5'
case_depend = '6'
data_depend = '7'
field_depend = '8'
data = '9'
expect = '10'
result = '11'
def get_id():
"""获取case_id"""
return global_val.Id
def get_request_name():
"""获取请求模块名称"""
return quest_name
def get_url():
"""获取请求url"""
return global_val.url
def get_run():
"""获取是否运⾏"""
return global_val.run
def get_run_way():
"""获取请求⽅式"""
return quest_way
def get_header():
"""获取是否携带header"""
return global_val.header
def get_case_depend():
"""case依赖"""
return global_val.case_depend
def get_data_depend():
"""依赖的返回数据"""
return global_val.data_depend
def get_field_depend():
"""数据依赖字段"""
return global_val.field_depend
def get_data():
"""获取请求数据"""
return global_val.data
def get_expect():
"""获取预期结果"""
return pect
def get_result():
"""获取返回结果"""
return sult
3、-----data
-----dependent_data.py
dependent_data:解决数据依赖问题from util.operation_excel import OperationExcel from base.runmethod import RunMethod
_data import GetData
from jsonpath_rw import jsonpath, parse import json
class DependentData:
"""解决数据依赖问题"""
def __init__(self, case_id):
self.case_id = case_id
self.opera_excel = OperationExcel()
self.data = GetData()
def get_case_line_data(self):
"""
通过case_id去获取该case_id的整⾏数据
:param case_id: ⽤例ID
:return:
"""
rows_data = self._row_data(self.case_id)
return rows_data
def run_dependent(self):
"""
执⾏依赖测试,获取结果
:return:
"""
run_method = RunMethod()
row_num = self._row_num(self.case_id)
request_data = _data_for_json(row_num)
# header = self.data.is_header(row_num)
method = _request_method(row_num)
url = _request_url(row_num)
res = run_method.run_main(method, url, request_data)
return json.loads(res)
def get_data_for_key(self, row):
"""
根据依赖的key去获取执⾏依赖case的响应然后返回
:return:
"""
depend_data = _depend_key(row)
response_data = self.run_dependent()
return [match.value for match in parse(depend_data).find(response_data)][0] 4、-----data
-----get_data.py
get_data:获取excel数据
from util.operation_excel import OperationExcel
from data import data_config
from util.operation_json import OperationJson
class GetData:
"""获取excel数据"""
def __init__(self):
self.opera_excel = OperationExcel()
def get_case_lines(self):
"""获取excel⾏数,即case的个数"""
return self._lines()
def get_is_run(self, row):
"""获取是否执⾏"""
flag = None
col = int(_run())
run_model = self._cell_value(row, col)
if run_model == 'yes':
flag = True
else:
flag = False
return flag
def is_header(self, row):
"""
是否携带header
:param row: ⾏号
:return:
"""
col = int(_header())
header = self._cell_value(row, col)
if header != '':
return header
else:
return None
def get_request_method(self, row):
"""
获取请求⽅式
:param row: ⾏号
:return:
"""
# col 列
col = int(_run_way())
request_method = self._cell_value(row, col)
return request_method
def get_request_url(self, row):
"""
获取url
:param row: ⾏号
:return:
"""
col = int(_url())
url = self._cell_value(row, col)
return url
def get_request_data(self, row):
"""
获取请求数据
:
param row:⾏号
:return:
"""
col = int(_data())
data = self._cell_value(row, col)
if data == '':
return None
return data
def get_data_for_json(self, row):
"""
通过关键字拿到data数据
:
param row:
:return:
"""
opera_json = OperationJson()
request_data = __request_data(row))    return request_data
def get_expcet_data(self, row):
"""
获取预期结果
:param row:
:return:
"""
col = int(_expect())
expect = self._cell_value(row, col)
if expect == "":
return None
else:
return expect
def write_result(self, row, value):
"""
写⼊结果数据
:param row:
:param col:
:
return:
"""
col = int(_result())
self.opera_excel.write_value(row, col, value)
def get_depend_key(self, row):
"""
获取依赖数据的key
:param row:⾏号
:return:
"""
col = int(_data_depend())
depend_key = self._cell_value(row, col)
if depend_key == "":
return None
else:
return depend_key
def is_depend(self, row):
"""
判断是否有case依赖
:param row:⾏号
:return:
"""
col = int(_case_depend()) # 获取是否存在数据依赖列
depend_case_id = self._cell_value(row, col)
if depend_case_id == "":
return None
else:
return depend_case_id
def get_depend_field(self, row):
"""
获取依赖字段
:param row:
:return:
"""
col = int(_field_depend())
data = self._cell_value(row, col)
if data == "":
return None
else:
return data
5、-----dataconfig
-----case.xls
case.xls:⽤例数据
6、-----dataconfig
-
----data.json
data.json:请求数据,根据⾃⼰实际业务,且与case层的请求数据列是关联的{
"user": {
"username": "1111111",
"password": "123456"
},
"filtrate": {
"type_id": "2",
"brand_id": "1",
"model_id": "111"
},
python怎么读取json文件
"search": {
"page": "1",
"keyword": "oppo",
"type": "12"
},
"token": {
"token": ""
}
7、-----dataconfig
-----token.json
token.json:实时⾃动将获取的token写⼊到该⽂件
{"data": {"token": "db6f0abee4e5040f5337f5c47a82879"}}
8、-----main

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。