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

如何让Bitbucket Pipelines测试标签显示JUnit报告的<system-out>与<system-err>?

在Bitbucket Pipelines测试标签中显示和内容

问题

已按Bitbucket官方文档配置Pipelines测试,生成的JUnit测试报告包含<system-out>和<system-err>控制台输出,但Bitbucket测试标签会自动排除这两部分,仅展示<failure>内的栈追踪内容,且未找到Bitbucket官方配置项调整此行为。

测试报告示例:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
  <testsuite name="pytest" errors="0" failures="1" skipped="0" tests="1" time="3.402" timestamp="2023-01-28T20:38:30.862709" hostname="6745580f0e58">
    <testcase classname="tests.mytest" name="test_force_error" time="0.090">
      <failure message="ValueError: Some error.">Traceback (most recent call last):
  File "/workspaces/app/tests/mytest.py", line 29, in test_force_error
    assert blah.blah(
  File "/workspaces/app/tests/mytest.py", line 91, in blah
    raise ValueError(
ValueError: Some error.</failure>
      <system-out>--------------------------------- Captured Log ---------------------------------

--------------------------------- Captured Out ---------------------------------
I WANT TO SEE THIS IN THE TEST TAB

</system-out>
      <system-err>--------------------------------- Captured Err ---------------------------------
WARNING: I WANT TO SEE STDERR TOO

</system-err>
    </testcase>
  </testsuite>
</testsuites>

解决方案

Bitbucket Pipelines测试报告UI目前不支持直接展示<system-out>和<system-err>,可通过修改JUnit报告生成逻辑绕过限制:

方法:修改pytest的JUnit报告输出逻辑

在项目根目录创建conftest.py文件,添加以下代码,自动将<system-out>和<system-err>内容追加到<failure>的错误信息末尾(仅针对失败用例):

from xml.etree import ElementTree as ET
import os

def pytest_sessionfinish(session, exitstatus):
    # 定位JUnit报告文件路径
    junit_dir = session.config.option.junit_xml_output_dir or "."
    junit_path = os.path.join(junit_dir, "junit.xml")
    if not os.path.exists(junit_path):
        return
    
    # 解析并修改XML内容
    tree = ET.parse(junit_path)
    root = tree.getroot()
    
    for testsuite in root.findall("testsuite"):
        for testcase in testsuite.findall("testcase"):
            output_content = ""
            # 收集标准输出
            system_out = testcase.find("system-out")
            if system_out is not None:
                output_content += "\n--- 捕获的STDOUT ---\n" + (system_out.text or "")
                testcase.remove(system_out)
            # 收集标准错误
            system_err = testcase.find("system-err")
            if system_err is not None:
                output_content += "\n--- 捕获的STDERR ---\n" + (system_err.text or "")
                testcase.remove(system_err)
            
            # 将输出追加到失败信息中
            failure = testcase.find("failure")
            if failure is not None and output_content:
                failure.text = (failure.text or "") + output_content
    
    # 保存修改后的报告
    tree.write(junit_path, encoding="utf-8", xml_declaration=True)

效果

运行测试后,生成的JUnit报告会将捕获的STDOUT和STDERR内容合并到<failure>标签内,Bitbucket测试标签会完整展示这部分内容,无需跳转查看完整测试输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:56:13