pytest html报告
pytest-html是pytest用于生成测试结果的html插件。可以登录github下载,可以使用pip进行安装。
pip安装命令:pip install pytest-html
以下是pytest-html通过pip安装的截图

运行html获取报告
执行语句:pytest --html=report.html

report.html

指定路径存放
命令:pytest --html=./assert/report.html


独立显示报告
独立显示报告是为了分享报告时,界面依旧能按照原样式显示。
命令: pytest --html=report.html --self-contained-html

pytest错误截图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # conftest.py # coding:utf-8 import time from selenium import webdriver import pytest driver = None @pytest.mark.hookwrapper def pytest_runtest_makereport(item): pytest_html = item.config.pluginmanager.getplugin( 'html' ) outcome = yield report = outcome.get_result() extra = getattr(report, 'extra' , []) if report.when == 'call' or report.when == "setup" : xfail = hasattr(report, 'wasxfail' ) if (report.skipped and xfail) or (report.failed and not xfail): file_name = report.nodeid.replace( "::" , "_" ) + ".png" screen_img = _capture_screenshot() if file_name: html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \ 'onclick="window.open(this.src)" align="right"/></div>' % screen_img extra.append(pytest_html.extras.html(html)) report.extra = extra report.description = str(item.function.__doc__) def _capture_screenshot(): return driver.get_screenshot_as_base64() @pytest.fixture(scope= 'session' , autouse=True) def browser(request): global driver if driver is None: driver = webdriver.Firefox() def end(): driver.quit() request.addfinalizer(end) return driver |
1 2 3 4 5 6 7 | # test_1.py import time def test_yoyo_01(browser): browser. get ( "https://www.cnblogs.com/qmm-1000/" ) time.sleep(2) t = browser.title assert t == "乌醍" |
1 2 3 4 5 6 | # test_02.py def test_02(browser): browser. get ( "https://www.cnblogs.com/qmm-1000/" ) time.sleep(2) t = browser.title assert '乌醍' in t |

pytest 失败重跑
失败重跑需要借助pytest-rerunfailures插件
下载指令:
pip install pytest-rerunfailures
失败重跑命令:
pytest --rerun 1 --html=report.html --self-contained-html (rerun后写几次运行几次)

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报