From 3e8bc5b0ddb03a61b14cd1c518f57189e2eab5d7 Mon Sep 17 00:00:00 2001 From: floraachy <1622042529@qq.com> Date: Wed, 17 May 2023 17:14:10 +0800 Subject: [PATCH] =?UTF-8?q?(2023-04-06)=20=E6=B7=BB=E5=8A=A0=E8=A3=85?= =?UTF-8?q?=E9=A5=B0=E5=99=A8add=5Fdocstring=EF=BC=8C=20=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E7=9A=84=E5=BE=80=E6=B5=8B=E8=AF=95=E6=96=B9=E6=B3=95=E4=B8=AD?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E4=BE=8B=E6=A0=87=E9=A2=98=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E6=96=87=E6=A1=A3=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=88=B0=E6=B5=8B=E8=AF=95=E6=8A=A5=E5=91=8A?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common_utils/func_handle.py | 18 ++++++++++++++ config/case_template.txt | 47 +++++++++++++++++++++---------------- conftest.py | 11 ++------- 3 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 common_utils/func_handle.py diff --git a/common_utils/func_handle.py b/common_utils/func_handle.py new file mode 100644 index 0000000..a5df01e --- /dev/null +++ b/common_utils/func_handle.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# @Time : 2023/4/6 11:36 +# @Author : Flora.Chen +# @File : helper.py +# @Software: PyCharm +# @Desc: + +def add_docstring(docstring): + """ + 函数装饰器,它接受一个字符串参数docstring, + 并返回一个装饰器函数。装饰器函数接受一个函数参数func, + 并将func的__doc__属性设置为docstring。 + """ + def decorator(func): + func.__doc__ = docstring + return func + + return decorator \ No newline at end of file diff --git a/config/case_template.txt b/config/case_template.txt index b3a6105..b9e1417 100644 --- a/config/case_template.txt +++ b/config/case_template.txt @@ -3,7 +3,9 @@ from case_utils.assert_handle import assert_result from case_utils.requests_handle import BaseRequest from loguru import logger from case_utils.request_data_handle import RequestPreDataHandle -from config.global_vars import GLOBAL_VARS +from pytest_html import extras # 往pytest-html报告中填写额外的内容 +from common_utils.func_handle import add_docstring + @pytest.fixture(params=${case_data}) def case(request): @@ -12,25 +14,30 @@ def case(request): """ return request.param - @pytest.mark.${func_title} @pytest.mark.auto -class ${class_title}Auto: - def ${func_title}_auto(self, case): - logger.info("-----------------------------START-开始执行用例-----------------------------") - logger.debug(f"当前执行的用例数据:{case}") - # 将用例标题title作为全局变量,方便后续写入到测试报告report.description中 - GLOBAL_VARS["title"] = case.get("title", "") - if case.get("run", None): - # 获取处理完成的用例数据 - case_data = RequestPreDataHandle(case).request_data_handle() - # 发送请求 - response = BaseRequest.send_request(case_data) - # 进行断言 - assert_result(response, case_data["expected"]) - else: - reason = f"标记了该用例为false,不执行\\n" - logger.warning(f"{reason}") - pytest.skip(reason) - logger.info("------------------------------------------用例执行结束------------------------------------------") +def ${func_title}_auto(case, extra): + """ + + """ + logger.info("-----------------------------START-开始执行用例-----------------------------") + logger.debug(f"当前执行的用例数据:{case}") + # 给当前测试方法添加文档注释 + add_docstring(case.get("title", ""))(${func_title}_auto) + if case.get("run", None): + # 获取处理完成的用例数据 + case_data = RequestPreDataHandle(case).request_data_handle() + # 将用例数据显示在pytest-html报告中 + extra.append(extras.text(str(case_data), name="用例数据")) + # 发送请求 + response = BaseRequest.send_request(case_data) + # 将响应数据显示在pytest-html报告中 + extra.append(extras.text(str(response.text), name="响应数据")) + # 进行断言 + assert_result(response, case_data["expected"]) + else: + reason = f"标记了该用例为false,不执行\\n" + logger.warning(f"{reason}") + pytest.skip(reason) + logger.info("------------------------------------------用例执行结束------------------------------------------") diff --git a/conftest.py b/conftest.py index 139470b..e9fd022 100644 --- a/conftest.py +++ b/conftest.py @@ -71,11 +71,8 @@ def pytest_runtest_makereport(item, call): report = outcome.get_result() # TODO:由于目前无法动态将用例数据中的title写入测试方法中的文档注释,因此该处理方法暂时搁置 # 将测试方法的文档注释作为结果表的Description的值,如果文档注释为空,则测试方法名作为结果表的Description的值 - # if item.function.__doc__ is None: - # report.description = str(item.function.__doc__) - # else: - # report.description = str(item.function.__name__) - report.description = GLOBAL_VARS.get("title", "") + logger.debug(f"文档注释:{item.function.__doc__}") + report.description = str(item.function.__doc__) report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape") @@ -123,8 +120,6 @@ def pytest_html_results_table_header(cells): cells.insert(0, html.th('用例描述', class_="sortable", col="name")) # 往表格中增加一列"执行时间",并且给"执行时间"增加排序 cells.insert(1, html.th('执行时间', class_="sortable time", col="time")) - # 移除表格最后一列 - cells.pop() @pytest.mark.optionalhook @@ -136,8 +131,6 @@ def pytest_html_results_table_row(report, cells): cells.insert(0, html.td(report.description)) # 往列"执行时间"插入每行的值 cells.insert(1, html.td(strftime("%Y-%m-%d %H:%M:%S"), class_="col-time")) - # 移除表格最后一列 - cells.pop() def pytest_html_results_table_html(report, data):