(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
|
||||
@@ -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,20 +14,25 @@ def case(request):
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.mark.${func_title}
|
||||
@pytest.mark.auto
|
||||
class ${class_title}Auto:
|
||||
def ${func_title}_auto(self, case):
|
||||
def ${func_title}_auto(case, extra):
|
||||
"""
|
||||
|
||||
"""
|
||||
logger.info("-----------------------------START-开始执行用例-----------------------------")
|
||||
logger.debug(f"当前执行的用例数据:{case}")
|
||||
# 将用例标题title作为全局变量,方便后续写入到测试报告report.description中
|
||||
GLOBAL_VARS["title"] = case.get("title", "")
|
||||
# 给当前测试方法添加文档注释
|
||||
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:
|
||||
|
||||
+2
-9
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user