社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
pytest --html=report.html --self-contained-html
1. 安装 pytest-html
运行: pytest scripts【your pytest scripts】 --html=report_default.html 【your html name and path】,报错,
pip3 install pytest-html 安装即可:
2 安装完后再次执行pytest脚本
pytest scripts --html=report_default.html
3 查看 生成的html
firefox report_default.html
4.发现用例名称没有显示,只显示了函数名称,增加一个 conftest.py 放在运行的目录下,conftest.py 放以下内容【让显示用例名称】,更多个性化编辑,如插入截图等请参考官网 https://pypi.org/project/pytest-html/
from datetime import datetime
from py.xml import html
import pytest
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(2, html.th('Description'))
cells.insert(1, html.th('Time', class_='sortable time', col='time'))
cells.pop()
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(2, html.td(report.description))
cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
cells.pop()
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
# report.description = str(item.function.__doc__)
report.description = str(item.function.__doc__)
# @pytest.hookimpl(hookwrapper=True)
# def pytest_runtest_makereport(item, call):
# pytest_html = item.config.pluginmanager.getplugin('html')
# outcome = yield
# report = outcome.get_result()
# extra = getattr(report, 'extra', [])
# if report.when == 'call':
# # always add url to report
# extra.append(pytest_html.extras.text(item.function.text_plant))
# # xfail = hasattr(report, 'wasxfail')
# # if (report.skipped and xfail) or (report.failed and not xfail):
# # # only add additional html on failure
# # extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
# report.extra = extra
5 这样就把用例的名字放了进来
6 注意 用例的名称就是 函数的说明信息
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!