【问题标题】:Is there a way to know when pytest-html create the actual html file?有没有办法知道 pytest-html 何时创建实际的 html 文件?
【发布时间】:2020-06-22 03:45:06
【问题描述】:

我想知道在通过 pytest 运行测试后何时创建 html 和资产文件夹。有没有办法知道文件的实际创建时间?我曾尝试在pycharm中使用断点,但无法获取文件实际创建的时间。

知道文件何时创建的全部意义在于我想将它复制到一个 zip 文件中。

【问题讨论】:

  • 测试完成后为什么不这样做?
  • 好点;主要是将压缩过程保留在实际测试文件中。因为我需要一直收集文件并压缩它们,所以我认为在 pytest 完成后编写压缩函数并将所有代码封装在测试套件所在的实际模块中是有意义的。
  • 不要尝试将管道放入同一管道的一部分。使用 shell 脚本,或者如果它变得复杂,则使用任何 CI/CD 解决方案。
  • 感谢您的建议。我以为有一种简单的方法可以让 pytest 在创建 html 报告后进行最后一次操作,但最终似乎并不是那么简单

标签: python pytest pytest-html


【解决方案1】:

HTML 报告写在pytest_sessionfinish hookimpl 中:

def pytest_sessionfinish(self, session):
    report_content = self._generate_report(session)
    self._save_report(report_content)

Source.

如果您想在自己的测试运行中操作报告文件,可以通过添加自己的 pytest_sessionfinish hookimpl 来实现,例如

import pathlib
import zipfile


def pytest_sessionfinish(session):
    htmlfile = session.config.getoption('htmlpath')
    if htmlfile is None:  # html report not wanted by user
        return
    htmlzip = pathlib.Path(htmlfile).with_suffix('.zip')
    with zipfile.ZipFile(htmlzip, 'w') as zip:
        zip.write(htmlfile)
        zip.write('assets/style.css')

【讨论】:

  • 太棒了,非常感谢!我写了一个钩子,但它是在测试完成时触发的,而不是在创建报告时触发的;因此,我的输出包括除实际 HTML 报告之外的所有文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多