增加主运行文件run.py

This commit is contained in:
zy7y 2020-12-16 11:01:31 +08:00
parent d2665b5bc6
commit 3e0550186c
6 changed files with 57 additions and 44 deletions

View File

@ -113,7 +113,7 @@ https://www.bilibili.com/video/BV1EE411B7SU?p=10
#### 测试报告
![本地运行测试后生成报告](./image/localhost_report.png)
![测试报告用例失败重跑](./image/用例失败重跑截图.png)
#### 更新
2020/08/08 增加实际响应存储数据的方法并在字典可以处理依赖见tools/svae_response.py
@ -129,6 +129,7 @@ https://www.bilibili.com/video/BV1EE411B7SU?p=10
2020/12/08 优化断言信息增加数据库支持mysql查询操作 使用`@pytest.fixture(scope="session")`来托管数据库对象用例新增sql栏
2020/12/16 使用conftest.py 初始化用例, 增加失败重跑机制, 增加运行文件run优化test_api.py冗余代码
#### 博客园首发
https://www.cnblogs.com/zy7y/p/13426816.html

View File

@ -17,9 +17,9 @@ expr:
token: $.data.token
file_path:
test_case: ../data/case_data.xlsx
report: ../report/
log: ../log/run{time}.log
test_case: data/case_data.xlsx
report: report/
log: log/run{time}.log
email:
# 发件人邮箱

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

37
run.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
"""
@project: apiAutoTest
@author: zy7y
@file: run.py
@ide: PyCharm
@time: 2020/12/16
@github: https://github.com/zy7y
@site: https://cnblogs.com/zy7y
@desc: 运行文件
"""
import os
import shutil
from test.conftest import pytest
from tools import logger
from tools.read_file import ReadFile
report = ReadFile.read_config('$.file_path.report')
logfile = ReadFile.read_config('$.file_path.log')
def run():
if os.path.exists('report/'):
shutil.rmtree(path='report/')
logger.add(logfile, enqueue=True, encoding='utf-8')
logger.info('开始测试...')
pytest.main(args=['test/test_api.py', f'--alluredir={report}/data'])
os.system(f'allure generate {report}/data -o {report}/html --clean')
logger.success('报告已生成')
if __name__ == '__main__':
run()

View File

@ -8,46 +8,22 @@
@time: 2020/11/22
@desc: 测试方法
"""
import os
import shutil
from .conftest import pytest
import pytest
from tools import logger
from api.base_requests import BaseRequest
from tools.data_process import DataProcess
from tools.read_file import ReadFile
report = ReadFile.read_config('$.file_path.report')
logfile = ReadFile.read_config('$.file_path.log')
class TestApi:
@classmethod
def run(cls):
if os.path.exists('../report'):
shutil.rmtree(path='../report')
logger.add(logfile, enqueue=True, encoding='utf-8')
logger.info('开始测试...')
pytest.main(args=[f'--alluredir={report}/data'])
os.system(f'allure generate {report}/data -o {report}/html --clean')
logger.success('报告已生成')
# https://www.cnblogs.com/shouhu/p/12392917.html
# reruns 重试次数 reruns_delay 次数之间的延时设置(单位:秒)
# 失败重跑,会影响总测试时长,如不需要 将 @pytest.mark.flaky(reruns=3, reruns_delay=5) 注释即可
@pytest.mark.flaky(reruns=3, reruns_delay=5)
def test_main(self, cases, get_db):
# 此处的cases入参来自与 conftest.py 文件中 cases函数与直接使用 @pytest.mark.parametrize
# 有着差不多的效果
# 发送请求
response, expect, sql = BaseRequest.send_request(cases)
# 执行sql
DataProcess.handle_sql(sql, get_db)
# 断言操作
DataProcess.assert_result(response, expect)
if __name__ == '__main__':
TestApi.run()
# https://www.cnblogs.com/shouhu/p/12392917.html
# reruns 重试次数 reruns_delay 次数之间的延时设置(单位:秒)
# 失败重跑,会影响总测试时长,如不需要 将 @pytest.mark.flaky(reruns=3, reruns_delay=5) 注释即可
# @pytest.mark.flaky(reruns=2, reruns_delay=1)
def test_main(cases, get_db):
# 此处的cases入参来自与 conftest.py 文件中 cases函数与直接使用 @pytest.mark.parametrize
# 有着差不多的效果
# 发送请求
response, expect, sql = BaseRequest.send_request(cases)
# 执行sql
DataProcess.handle_sql(sql, get_db)
# 断言操作
DataProcess.assert_result(response, expect)

View File

@ -17,7 +17,7 @@ class ReadFile:
config_dict = None
@classmethod
def get_config_dict(cls, config_path: str = '../config/config.yaml') -> dict:
def get_config_dict(cls, config_path: str = 'config/config.yaml') -> dict:
"""读取配置文件,并且转换成字典
:param config_path: 配置文件地址 默认使用当前项目目录下的config/config.yaml
return cls.config_dict
@ -53,4 +53,3 @@ class ReadFile:
value.pop(3)
data_list.append(list(value))
return data_list