pytest学习和使用-HTML报告如何生成?(pytest-html)_from py
warning:
这篇文章距离上次修改已过187天,其中的内容可能已经有所变动。
在Pytest中,你可以使用pytest-html
插件来生成HTML格式的测试报告。以下是如何使用该插件的步骤:
- 安装
pytest-html
插件:
pip install pytest-html
- 使用命令行参数
--html
或者配置文件中的pytest_html
选项来生成报告:
命令行方式:
pytest --html=report.html
配置文件方式(pytest.ini
):
[pytest]
addopts = --html=report.html
- 运行你的测试用例,之后在当前目录下会生成一个
report.html
的报告文件。
如果你想要自定义报告的标题、样式等,可以在调用pytest
时指定--html
参数后面的路径为一个Python字典,例如:
pytest --html=report.html --self-contained-html
这将生成一个自包含的HTML报告,包含所有必须的资源。如果你想要更详细的配置,可以在Python脚本中使用pytest.main()
函数,并传递配置字典:
import pytest
pytest.main([
'--html=report.html',
'--html-report-stylesheet=my_style.css',
'tests/'
])
在这个例子中,my_style.css
是你提前准备的CSS样式表,它将覆盖默认的样式来定制你的HTML报告。
评论已关闭