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 pytest
|
|
|
|
|
from api.base_requests import BaseRequest
|
2020-11-21 17:40:59 +08:00
|
|
|
|
from tools import *
|
2020-11-19 00:49:26 +08:00
|
|
|
|
from tools.data_process import DataProcess
|
2020-11-21 17:40:59 +08:00
|
|
|
|
from tools.read_file import ReadFile
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
2020-11-21 17:40:59 +08:00
|
|
|
|
base_url = ReadFile.read_config('$.server.dev')
|
|
|
|
|
res_reg = ReadFile.read_config('$.expr.response')
|
|
|
|
|
report_data = ReadFile.read_config('$.file_path.report_data')
|
|
|
|
|
report_generate = ReadFile.read_config('$.file_path.report_generate')
|
|
|
|
|
log_path = ReadFile.read_config('$.file_path.log_path')
|
2020-08-10 16:24:24 +08:00
|
|
|
|
# 读取excel数据对象
|
2020-11-21 17:40:59 +08:00
|
|
|
|
data_list = ReadFile.read_testcase()
|
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 打开会出现无数据情况
|
2020-11-20 20:09:12 +08:00
|
|
|
|
os.system(f'allure generate {report_data} -o {report_generate} --clean')
|
2020-09-11 17:58:43 +08:00
|
|
|
|
|
|
|
|
|
# 直接启动allure报告(会占用一个进程,建立一个本地服务并且自动打开浏览器访问,ps 程序不会自动结束,需要自己去关闭)
|
2020-11-20 20:09:12 +08:00
|
|
|
|
# os.system(f'allure serve {report_data}')
|
2020-08-10 17:03:21 +08:00
|
|
|
|
logger.warning('报告已生成')
|
|
|
|
|
|
2020-11-20 23:55:22 +08:00
|
|
|
|
@pytest.mark.parametrize('case_number,case_title,path,is_token,method,parametric_key,file_obj,'
|
2020-11-20 20:09:12 +08:00
|
|
|
|
'data,expect', data_list)
|
2020-11-20 23:55:22 +08:00
|
|
|
|
def test_main(self, case_number, case_title, path, is_token, method, parametric_key, file_obj,
|
2020-11-20 20:09:12 +08:00
|
|
|
|
data, expect):
|
2020-08-10 17:03:21 +08:00
|
|
|
|
logger.debug(f'⬇️⬇️⬇️...执行用例编号:{case_number}...⬇️⬇️⬇️️')
|
2020-08-13 16:37:11 +08:00
|
|
|
|
|
2020-11-21 17:40:59 +08:00
|
|
|
|
allure_title(case_title)
|
|
|
|
|
path = DataProcess.handle_path(path)
|
|
|
|
|
allure_step('请求地址', base_url + path)
|
|
|
|
|
allure_step('请求方式', method)
|
|
|
|
|
header = DataProcess.handle_header(is_token)
|
|
|
|
|
allure_step('请求头', header)
|
|
|
|
|
data = DataProcess.handle_data(data)
|
|
|
|
|
allure_step('请求数据', data)
|
|
|
|
|
res = br.api_send(method=method, url=base_url + path, parametric_key=parametric_key, file_obj=file_obj,
|
|
|
|
|
data=data, header=header)
|
|
|
|
|
allure_step('实际响应结果', res)
|
|
|
|
|
DataProcess.save_response(case_number, res)
|
|
|
|
|
allure_step('响应结果写入字典', DataProcess.response_dict)
|
|
|
|
|
# 写token的接口必须是要正确无误能返回token的
|
|
|
|
|
if is_token == '写':
|
|
|
|
|
DataProcess.have_token['Authorization'] = extractor(res, ReadFile.read_config('$.expr.token'))
|
|
|
|
|
really = extractor(res, res_reg)
|
|
|
|
|
allure_step('根据配置文件的提取响应规则提取实际数据', really)
|
|
|
|
|
expect = convert_json(expect)
|
|
|
|
|
allure_step('处理读取出来的预期结果响应', expect)
|
|
|
|
|
allure_step('响应断言', (really == expect))
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|