由原生的报告思考问题:
根据默认的命令,生成的报告均为英文,不适合广泛阅读?
报告中三个模块:Environment、Summary、Results?
从官方文档中,可以找到修改的方法:
官方文档链接:https://pypi.org/project/pytest-html/
前提:
当前使用版本:python3.7.5 ,pytest 5.3.5 pytest-html2.0.1
配置conftest.py文件(该文件为pytest项目下的fixture集中配置地方),有兴趣的同学,可以深入pytest.fixture(https://docs.pytest.org/en/latest/contents.html)
操作conftest.py:
from datetimeimport datetime
import pytest
from py.xmlimport html
# 修改ENviroment的内容
def pytest_configure(config):
# 添加接口地址与项目名称
config._metadata["项目名称"] ="测试pytest项目"
config._metadata["测试case地址"] ="D:\python\Apitest\test_Case"
# 删除java_Home
config._metadata.pop("JAVA_HOME")
config._metadata.pop("Python")
config._metadata.pop("Packages")\
# 修改summary内容
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
prefix.extend([html.p("所属部门:xx测试组")])
prefix.extend(([html.p("测试人员:欧阳二蛋")]))
# 修改result_table中内容
def pytest_html_results_table_header(cells):
cells.insert(2, html.th('Description'))
cells.insert(3, html.th('Time', class_='sortable time', col='time'))
cells.pop()
def pytest_html_results_table_row(report, cells):
cells.insert(2, html.td(report.description))
cells.insert(3, html.td(datetime.utcnow(), class_='col-time'))
cells.pop()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome =yield
report = outcome.get_result()
report.description =str(item.function.__doc__)
report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")#设置编码显示中文
最终结果展示:
Tips:
文档中还有图片、文件等操作的相关操作,有兴趣的可以深入了解下。