我将 pytest 与pytest-html插件结合使用,该插件在测试运行后生成 HTML 报告。
我正在使用自动连接的会话夹具在浏览器中自动打开生成的 HTML 报告:
@pytest.fixture(scope="session", autouse=True)
def session_wrapper(request):
print('Session wrapper init...')
yield
# open report in browser on Mac or Windows, skip headless boxes
if platform.system() in ['Darwin', 'Windows']:
html_report_path = os.path.join(request.config.invocation_dir.strpath, request.config.option.htmlpath)
open_url_in_browser("file://%s" %html_report_path)
上面的代码有效,但不一致,因为有时浏览器会在文件创建之前尝试加载文件,这会导致文件未找到错误,并且需要手动刷新浏览器才能显示报告。
我的理解是这scope="session"
是最广泛的可用范围,我的假设是 pytest-html 应该在会话结束之前完成生成报告,但显然情况并非如此。
问题是:挂钩浏览器报告自动启动代码的正确方法是什么?难道这pytest-html
也与会话终结器范围挂钩?在这种情况下,如何确保 HTML 文件仅在文件创建后才在浏览器中打开?