You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pytest HTML报告无法捕获各测试用例输出问题咨询

问题:Pytest HTML报告显示“No log output captured.”

我使用以下代码配置Pytest生成HTML报告:

# define a hook to add the environment info to the HTML Report
def pytest_configure (config):
    config._metadata['Project Name'] = 'Networks'
    config._metadata['Module Name'] = 'Networks Automation'
    config._metadata['Tester'] = 'Greg Williams'

# define a hook to delete/modify the environment info in the HTML Report
@pytest.hookimpl(optionalhook=True)
def pytest_metadata(metadata):
    metadata.pop("JAVA_HOME", None)
    metadata.pop("Plugins", None)

在PyCharm终端执行命令:

pytest -s -v --html=Reports/report.html testCases/test_login.py --browser chrome

测试用例运行通过且report.html已生成,但每个测试用例下方显示‘No log output captured.’,截图如下:
测试报告无日志输出截图


解决方案

核心原因

你使用的-s参数等价于--capture=no,会禁用Pytest的输出捕获功能,导致HTML报告无法抓取测试用例的标准输出、错误输出或日志内容,因此提示“No log output captured.”。

具体解决办法

  1. 替换-s为--capture=tee-sys
    该参数可同时在终端输出内容,并将输出捕获到报告中,兼顾实时查看和报告记录需求。修改后的命令:

    pytest -v --html=Reports/report.html --capture=tee-sys testCases/test_login.py --browser chrome
    
  2. 若必须保留-s,显式使用日志模块记录
    在测试用例中通过logging模块打印日志,确保内容被Pytest捕获并写入报告:

    import logging
    
    def test_login():
        logging.info("启动Chrome浏览器,开始登录流程")
        # 你的测试步骤代码
        logging.info("登录验证通过,测试用例执行完成")
    

    同时需在命令行添加日志级别配置,比如--log-level=INFO,或在pytest.ini中统一设置。

  3. 更新pytest-html插件
    旧版本插件可能存在捕获逻辑bug,执行以下命令升级到最新版:

    pip install --upgrade pytest-html
    

内容的提问来源于stack exchange,提问作者chauhany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:42:51