首页 > 解决方案 > 如何将 pytest-html-reporter 生成的报告存储到自定义位置

问题描述

我正在使用 pytest-html-reporter 0.2.4 插件生成执行报告。当我直接传递命令行参数“--html-report=./report/report.html”时,我能够在给定位置获取报告。

我想将报告存储在一个单独的位置,执行日期(YYYYMMDD)作为文件夹名称,report_HHMM.html 作为报告名称

这是我目前使用的逻辑

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    time_var = datetime.datetime.now()
    # create report target dir
    reports_dir = Path('reports', time_var.strftime('%Y%m%d'))
    reports_dir.mkdir(parents=True, exist_ok=True)
    # line to be add for addopts
    args = f"--html-report=./{reports_dir}/report_{time_var.strftime('%H%M')}.html"
    config.addinivalue_line("addopts", args)
    print("inside config function",config.getini("addopts"))

这是我的 pytest.ini 文件

[pytest]
python_files = test_*
python_functions = test_*
python_classes = *Tests
addopts =

使用 config.getini("addopts") 我可以看到报告路径被添加到 addopts,但是没有报告被添加到创建的目录

标签: python-3.xseleniumpytestpytest-html

解决方案


推荐阅读