前言
- 本篇来学习使用pytest-html插件生成HTML报告
安装包
pip install pytest-html
使用示例
- –html=report.html : css 文件和html报告是分开的
import os
def test_add():
a = 1 + 2
assert a == 3
if __name__ == '__main__':
os.system('pytest -s test_52.py --html=report.html')
- –self-contained-html : 合并css 到html报告中
import os
def test_add():
a = 1 + 2
assert a == 3
if __name__ == '__main__':
os.system('pytest -s test_52.py --html=report.html --self-contained-html')
- 查看HTML报告
修改HTML报告
- 添加描述(Description)列,添加可排序时间(Time)列,并删除链接(Link)列
- 新建conftest.py文件(文件名固定,不要改),代码如下
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__)
import os
import time
def test_add():
"""验证1+2等于3"""
a = 1 + 2
time.sleep(2)
assert a == 3
if __name__ == '__main__':
os.system('pytest -s test_53.py --html=./report/report.html --self-contained-html')
- 查看HTML报告