diff --git a/README.md b/README.md index ae8887d..775dbb3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config/config.yaml b/config/config.yaml index d4f0b50..4cbb85e 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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: # 发件人邮箱 diff --git a/image/用例失败重跑截图.png b/image/用例失败重跑截图.png new file mode 100644 index 0000000..8f06b41 Binary files /dev/null and b/image/用例失败重跑截图.png differ diff --git a/run.py b/run.py new file mode 100644 index 0000000..e5f444f --- /dev/null +++ b/run.py @@ -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() + + diff --git a/test/test_api.py b/test/test_api.py index cb3c721..18a8e72 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -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() \ No newline at end of file +# 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) diff --git a/tools/read_file.py b/tools/read_file.py index f6eebf3..7142c33 100644 --- a/tools/read_file.py +++ b/tools/read_file.py @@ -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 -