【问题标题】:How to store pytest-html-reporter generated report to a custom location如何将 pytest-html-reporter 生成的报告存储到自定义位置
【发布时间】:2022-04-13 23:27:36
【问题描述】:

我正在使用 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.x selenium pytest pytest-html pytest-html-reporter


    【解决方案1】:
    @pytest.hookimpl(tryfirst=True)
    def pytest_configure(config):
        if not os.path.exists('reports'):
            os.makedirs('reports')
        config.option.htmlpath = 'reports/report_' + datetime.now().strftime("%d-%m-%Y %H-%M-%S")+".html"
    

    来源: https://github.com/pytest-dev/pytest-html/issues/176

    【讨论】:

    • 我认为这个答案更具体到 pytest-html 库,但问题是针对 pytest-html-reporter
    【解决方案2】:

    如果你想把pytest-html-reporter 报告放在folderA 下,你可以试试这个

    pytest tests/ --html-report=./folderA/report.html
    pytest tests/ --html-report=./folderA
    

    或者您可以将它们直接放在pytest.ini 文件下

    [pytest]
    addopts = -vs -rf --html-report=./folderA
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      相关资源
      最近更新 更多