为何pytest HTML报告中未显示print语句与logging.info内容?
解决pytest HTML报告无法捕获print输出的问题
问题核心:你使用的-s参数会关闭pytest对标准输出(比如print内容)的捕获功能——虽然控制台能正常显示输出,但HTML报告因未拿到捕获的内容,所以点击「显示详情」时会出现"No Log Output Captured"提示。
直接可用的解决方案:
- 把
-s替换为--capture=tee-sys参数,这个参数会同时将输出发送到控制台并捕获给报告使用。修改后的执行命令为:pytest test_01.py -v --html=report.html --capture=tee-sys - 若不需要控制台实时输出,直接去掉
-s参数即可,pytest默认会捕获输出,报告里就能正常显示测试详情。
额外优化方案:
用pytest推荐的日志模块替代print,更规范且兼容性更强。比如在测试用例中添加:
import logging def test_verify_file_count(): logging.info("------------------------ Test 1 Initiated: Verify LogRetention file count in dropbox ------------------------") logging.info("Device rebooting...") # 测试逻辑代码... logging.info("------------------------ Test 1 PASSED successfully : Verify LogRetention file count in dropbox folder ------------------------")
执行时无需额外参数,日志内容会自动被捕获到HTML报告中;如果需要控制台也显示日志,可加上--log-cli-level=INFO参数:
pytest test_01.py -v --html=report.html --log-cli-level=INFO
内容的提问来源于stack exchange,提问作者ilexcel
相关产品推荐
相关产品推荐

