(2023-04-06)
添加装饰器add_docstring, 动态的往测试方法中添加用例标题作为文档注释,并输出到测试报告中
This commit is contained in:
@@ -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
|
||||||
+27
-20
@@ -3,7 +3,9 @@ from case_utils.assert_handle import assert_result
|
|||||||
from case_utils.requests_handle import BaseRequest
|
from case_utils.requests_handle import BaseRequest
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from case_utils.request_data_handle import RequestPreDataHandle
|
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})
|
@pytest.fixture(params=${case_data})
|
||||||
def case(request):
|
def case(request):
|
||||||
@@ -12,25 +14,30 @@ def case(request):
|
|||||||
"""
|
"""
|
||||||
return request.param
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.${func_title}
|
@pytest.mark.${func_title}
|
||||||
@pytest.mark.auto
|
@pytest.mark.auto
|
||||||
class ${class_title}Auto:
|
def ${func_title}_auto(case, extra):
|
||||||
def ${func_title}_auto(self, case):
|
"""
|
||||||
logger.info("-----------------------------START-开始执行用例-----------------------------")
|
|
||||||
logger.debug(f"当前执行的用例数据:{case}")
|
"""
|
||||||
# 将用例标题title作为全局变量,方便后续写入到测试报告report.description中
|
logger.info("-----------------------------START-开始执行用例-----------------------------")
|
||||||
GLOBAL_VARS["title"] = case.get("title", "")
|
logger.debug(f"当前执行的用例数据:{case}")
|
||||||
if case.get("run", None):
|
# 给当前测试方法添加文档注释
|
||||||
# 获取处理完成的用例数据
|
add_docstring(case.get("title", ""))(${func_title}_auto)
|
||||||
case_data = RequestPreDataHandle(case).request_data_handle()
|
if case.get("run", None):
|
||||||
# 发送请求
|
# 获取处理完成的用例数据
|
||||||
response = BaseRequest.send_request(case_data)
|
case_data = RequestPreDataHandle(case).request_data_handle()
|
||||||
# 进行断言
|
# 将用例数据显示在pytest-html报告中
|
||||||
assert_result(response, case_data["expected"])
|
extra.append(extras.text(str(case_data), name="用例数据"))
|
||||||
else:
|
# 发送请求
|
||||||
reason = f"标记了该用例为false,不执行\\n"
|
response = BaseRequest.send_request(case_data)
|
||||||
logger.warning(f"{reason}")
|
# 将响应数据显示在pytest-html报告中
|
||||||
pytest.skip(reason)
|
extra.append(extras.text(str(response.text), name="响应数据"))
|
||||||
logger.info("------------------------------------------用例执行结束------------------------------------------")
|
# 进行断言
|
||||||
|
assert_result(response, case_data["expected"])
|
||||||
|
else:
|
||||||
|
reason = f"标记了该用例为false,不执行\\n"
|
||||||
|
logger.warning(f"{reason}")
|
||||||
|
pytest.skip(reason)
|
||||||
|
logger.info("------------------------------------------用例执行结束------------------------------------------")
|
||||||
|
|
||||||
|
|||||||
+2
-9
@@ -71,11 +71,8 @@ def pytest_runtest_makereport(item, call):
|
|||||||
report = outcome.get_result()
|
report = outcome.get_result()
|
||||||
# TODO:由于目前无法动态将用例数据中的title写入测试方法中的文档注释,因此该处理方法暂时搁置
|
# TODO:由于目前无法动态将用例数据中的title写入测试方法中的文档注释,因此该处理方法暂时搁置
|
||||||
# 将测试方法的文档注释作为结果表的Description的值,如果文档注释为空,则测试方法名作为结果表的Description的值
|
# 将测试方法的文档注释作为结果表的Description的值,如果文档注释为空,则测试方法名作为结果表的Description的值
|
||||||
# if item.function.__doc__ is None:
|
logger.debug(f"文档注释:{item.function.__doc__}")
|
||||||
# report.description = str(item.function.__doc__)
|
report.description = str(item.function.__doc__)
|
||||||
# else:
|
|
||||||
# report.description = str(item.function.__name__)
|
|
||||||
report.description = GLOBAL_VARS.get("title", "")
|
|
||||||
report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
|
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(0, html.th('用例描述', class_="sortable", col="name"))
|
||||||
# 往表格中增加一列"执行时间",并且给"执行时间"增加排序
|
# 往表格中增加一列"执行时间",并且给"执行时间"增加排序
|
||||||
cells.insert(1, html.th('执行时间', class_="sortable time", col="time"))
|
cells.insert(1, html.th('执行时间', class_="sortable time", col="time"))
|
||||||
# 移除表格最后一列
|
|
||||||
cells.pop()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.optionalhook
|
@pytest.mark.optionalhook
|
||||||
@@ -136,8 +131,6 @@ def pytest_html_results_table_row(report, cells):
|
|||||||
cells.insert(0, html.td(report.description))
|
cells.insert(0, html.td(report.description))
|
||||||
# 往列"执行时间"插入每行的值
|
# 往列"执行时间"插入每行的值
|
||||||
cells.insert(1, html.td(strftime("%Y-%m-%d %H:%M:%S"), class_="col-time"))
|
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):
|
def pytest_html_results_table_html(report, data):
|
||||||
|
|||||||
Reference in New Issue
Block a user