2020-08-03 16:15:03 +08:00
|
|
|
|
#!/usr/bin/env/python3
|
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
@project: apiAutoTest
|
|
|
|
|
@author: zy7y
|
|
|
|
|
@file: test_api.py
|
|
|
|
|
@ide: PyCharm
|
|
|
|
|
@time: 2020/7/31
|
|
|
|
|
"""
|
|
|
|
|
import json
|
|
|
|
|
import jsonpath
|
2020-10-22 15:41:42 +08:00
|
|
|
|
from loguru import logger
|
2020-08-03 16:15:03 +08:00
|
|
|
|
import pytest
|
|
|
|
|
import allure
|
|
|
|
|
from api.base_requests import BaseRequest
|
2020-08-10 15:49:13 +08:00
|
|
|
|
from tools.data_tearing import TreatingData
|
2020-08-03 16:15:03 +08:00
|
|
|
|
from tools.read_config import ReadConfig
|
|
|
|
|
from tools.read_data import ReadData
|
2020-08-08 12:00:50 +08:00
|
|
|
|
from tools.save_response import SaveResponse
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
2020-08-10 16:24:24 +08:00
|
|
|
|
# 读取配置文件 对象
|
2020-08-03 16:15:03 +08:00
|
|
|
|
rc = ReadConfig()
|
|
|
|
|
base_url = rc.read_serve_config('dev')
|
|
|
|
|
token_reg, res_reg = rc.read_response_reg()
|
|
|
|
|
case_data_path = rc.read_file_path('case_data')
|
|
|
|
|
report_data = rc.read_file_path('report_data')
|
|
|
|
|
report_generate = rc.read_file_path('report_generate')
|
2020-08-12 01:56:58 +08:00
|
|
|
|
log_path = rc.read_file_path('log_path')
|
2020-08-03 16:15:03 +08:00
|
|
|
|
report_zip = rc.read_file_path('report_zip')
|
|
|
|
|
email_setting = rc.read_email_setting()
|
2020-08-08 12:00:50 +08:00
|
|
|
|
# 实例化存响应的对象
|
2020-08-09 18:32:31 +08:00
|
|
|
|
save_response_dict = SaveResponse()
|
2020-08-10 16:24:24 +08:00
|
|
|
|
# 读取excel数据对象
|
2020-08-11 17:27:19 +08:00
|
|
|
|
data_list = ReadData(case_data_path).get_data()
|
2020-08-10 15:49:13 +08:00
|
|
|
|
# 数据处理对象
|
|
|
|
|
treat_data = TreatingData()
|
2020-08-10 16:24:24 +08:00
|
|
|
|
# 请求对象
|
2020-08-03 16:15:03 +08:00
|
|
|
|
br = BaseRequest()
|
2020-08-12 01:56:58 +08:00
|
|
|
|
logger.info(f'配置文件/excel数据/对象实例化,等前置条件处理完毕\n\n')
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestApiAuto(object):
|
2020-08-10 17:03:21 +08:00
|
|
|
|
# 启动方法
|
|
|
|
|
def run_test(self):
|
|
|
|
|
import os, shutil
|
|
|
|
|
if os.path.exists('../report') and os.path.exists('../log'):
|
|
|
|
|
shutil.rmtree(path='../report')
|
|
|
|
|
shutil.rmtree(path='../log')
|
|
|
|
|
# 日志存取路径
|
2020-08-12 02:24:32 +08:00
|
|
|
|
logger.add(log_path, encoding='utf-8')
|
2020-08-10 17:03:21 +08:00
|
|
|
|
pytest.main(args=[f'--alluredir={report_data}'])
|
2020-09-11 17:58:43 +08:00
|
|
|
|
# 本地生成 allure 报告文件,需注意 不用pycharm等类似ide 打开会出现无数据情况
|
|
|
|
|
# os.system(f'allure generate {report_data} -o {report_generate} --clean')
|
|
|
|
|
|
|
|
|
|
# 直接启动allure报告(会占用一个进程,建立一个本地服务并且自动打开浏览器访问,ps 程序不会自动结束,需要自己去关闭)
|
|
|
|
|
os.system(f'allure serve {report_data}')
|
2020-08-10 17:03:21 +08:00
|
|
|
|
logger.warning('报告已生成')
|
|
|
|
|
|
2020-08-11 17:27:19 +08:00
|
|
|
|
@pytest.mark.parametrize('case_number,case_title,path,is_token,method,parametric_key,file_var,'
|
|
|
|
|
'file_path, parameters, dependent,data,expect', data_list)
|
2020-08-12 02:24:32 +08:00
|
|
|
|
def test_main(self, case_number, case_title, path, is_token, method, parametric_key, file_var,
|
|
|
|
|
file_path, parameters, dependent, data, expect):
|
2020-08-11 17:27:19 +08:00
|
|
|
|
|
2020-08-11 17:36:13 +08:00
|
|
|
|
# 感谢:https://www.cnblogs.com/yoyoketang/p/13386145.html,提供动态添加标题的实例代码
|
2020-08-11 17:27:19 +08:00
|
|
|
|
# 动态添加标题
|
|
|
|
|
allure.dynamic.title(case_title)
|
|
|
|
|
|
2020-08-10 17:03:21 +08:00
|
|
|
|
logger.debug(f'⬇️⬇️⬇️...执行用例编号:{case_number}...⬇️⬇️⬇️️')
|
2020-08-03 16:15:03 +08:00
|
|
|
|
with allure.step("处理相关数据依赖,header"):
|
2020-08-10 15:49:13 +08:00
|
|
|
|
data, header, parameters_path_url = treat_data.treating_data(is_token, parameters, dependent, data, save_response_dict)
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(header, ensure_ascii=False, indent=4), "请求头", allure.attachment_type.TEXT)
|
|
|
|
|
allure.attach(json.dumps(data, ensure_ascii=False, indent=4), "请求数据", allure.attachment_type.TEXT)
|
|
|
|
|
|
2020-08-03 16:15:03 +08:00
|
|
|
|
with allure.step("发送请求,取得响应结果的json串"):
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(base_url + path + parameters_path_url, ensure_ascii=False, indent=4), "最终请求地址", allure.attachment_type.TEXT)
|
2020-08-09 20:47:05 +08:00
|
|
|
|
res = br.base_requests(method=method, url=base_url + path + parameters_path_url, parametric_key=parametric_key, file_var=file_var, file_path=file_path,
|
2020-08-03 16:15:03 +08:00
|
|
|
|
data=data, header=header)
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(res, ensure_ascii=False, indent=4), "实际响应", allure.attachment_type.TEXT)
|
|
|
|
|
|
2020-08-10 17:03:21 +08:00
|
|
|
|
with allure.step("将响应结果的内容写入实际响应字典中"):
|
2020-08-09 18:32:31 +08:00
|
|
|
|
save_response_dict.save_actual_response(case_key=case_number, case_response=res)
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(save_response_dict.actual_response, ensure_ascii=False, indent=4), "实际响应字典", allure.attachment_type.TEXT)
|
|
|
|
|
|
2020-08-03 16:15:03 +08:00
|
|
|
|
# 写token的接口必须是要正确无误能返回token的
|
|
|
|
|
if is_token == '写':
|
|
|
|
|
with allure.step("从登录后的响应中提取token到header中"):
|
2020-08-10 15:49:13 +08:00
|
|
|
|
treat_data.token_header['Authorization'] = jsonpath.jsonpath(res, token_reg)[0]
|
2020-08-13 16:37:11 +08:00
|
|
|
|
|
2020-08-03 16:15:03 +08:00
|
|
|
|
with allure.step("根据配置文件的提取响应规则提取实际数据"):
|
|
|
|
|
really = jsonpath.jsonpath(res, res_reg)[0]
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(really, ensure_ascii=False, indent=4), "提取用于断言的实际响应部分数据", allure.attachment_type.TEXT)
|
|
|
|
|
|
2020-08-03 16:15:03 +08:00
|
|
|
|
with allure.step("处理读取出来的预期结果响应"):
|
2020-10-21 19:24:37 +08:00
|
|
|
|
# 处理预期结果数据中使用True/False/None导致的无法转换bug
|
|
|
|
|
if 'None' in expect:
|
|
|
|
|
expect = expect.replace('None', 'null')
|
|
|
|
|
if 'True' in expect:
|
|
|
|
|
expect = expect.replace('True', 'true')
|
|
|
|
|
if 'False' in expect:
|
|
|
|
|
expect = expect.replace('False', 'false')
|
|
|
|
|
expect = json.loads(expect)
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(expect, ensure_ascii=False, indent=4), "预期响应", allure.attachment_type.TEXT)
|
|
|
|
|
|
2020-08-03 16:15:03 +08:00
|
|
|
|
with allure.step("预期结果与实际响应进行断言操作"):
|
2020-08-10 17:03:21 +08:00
|
|
|
|
logger.info(f'完整的json响应: {res}\n需要校验的数据字典: {really} 预期校验的数据字典: {expect} \n测试结果: {really == expect}')
|
|
|
|
|
logger.debug(f'⬆⬆⬆...用例编号:{case_number},执行完毕,日志查看...⬆⬆⬆\n\n️')
|
2020-08-13 16:37:11 +08:00
|
|
|
|
allure.attach(json.dumps(really == expect, ensure_ascii=False, indent=4), "测试结果", allure.attachment_type.TEXT)
|
|
|
|
|
assert really == expect
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-08-10 17:03:21 +08:00
|
|
|
|
TestApiAuto().run_test()
|
2020-08-10 15:49:13 +08:00
|
|
|
|
|
2020-08-12 01:56:58 +08:00
|
|
|
|
# 使用jenkins集成将不会使用到这两个方法(邮件发送/报告压缩zip)
|
2020-08-08 12:00:50 +08:00
|
|
|
|
# from tools.zip_file import zipDir
|
|
|
|
|
# from tools.send_email import send_email
|
|
|
|
|
# zipDir(report_generate, report_zip)
|
|
|
|
|
# send_email(email_setting)
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|