Jenkins报告测试截图显示异常,本地报告正常如何解决?
解决Jenkins报告截图显示破损问题
1. 修正截图路径为相对路径
本地报告显示正常是因为使用了本地绝对路径,而Jenkins中报告通过Web访问,需要使用相对于HTML报告文件的路径。修改conftest.py中的代码:
修改pytest_runtest_makereport函数中的截图路径处理部分
在确认截图存在后,计算相对于Reports目录的相对路径,替换原有的绝对路径引用:
if screenshot_path.exists(): # 计算相对于Reports目录的相对路径 project_root = Path(__file__).resolve().parent.parent reports_dir = project_root / "Reports" relative_screenshot_path = screenshot_path.relative_to(reports_dir) # Embed screenshot in HTML report if pytest_html: html = ( f'<div><img src="{relative_screenshot_path}" alt="screenshot" ' f'style="width:304px;height:228px;" ' f'onclick="window.open(this.src)" align="right"/></div>' ) extra.append(pytest_html.extras.html(html))
2. 确认Jenkins构建产物配置
确保构建产物包含Reports目录下的所有文件(包括子目录),配置路径为:
Reports/**
这样Jenkins会归档HTML报告和对应的截图文件,保证Web访问时能加载到截图。
3. 验证路径一致性
- 运行Jenkins构建后,查看归档的
Reports目录,确认screenshots文件夹和截图文件存在 - 打开HTML报告,检查截图的
src属性是否为类似screenshots/test_case_name.png的相对路径
内容的提问来源于stack exchange,提问作者Solaf Kmail
相关产品推荐
相关产品推荐

