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

如何在pytest-html生成的HTML报告中隐藏失败测试用例的代码?

Fixing pytest-html Report to Show Only Error Statements (No Code Blocks) for Failed Tests

Hey there! I get it—you want your pytest-html reports (generated with Python 3.6 in PyCharm) to only display the error statement when a test fails, instead of cluttering things up with code blocks. Let’s walk through three practical solutions to make this happen:

Option 1: Use pytest's Traceback Shortening Flags (Quickest Fix)

The simplest way is to tell pytest to generate shorter tracebacks, which pytest-html will pick up automatically. Just add one of these flags when running your tests:

  • --tb=line: Shows each error as a single line with the core error message (no code snippets)
  • --tb=short: Truncates the traceback to exclude extra context, including code blocks

Run your tests with this command in PyCharm's terminal:

pytest --html=test_report.html --self-contained-html --tb=line

This is the fastest fix if you don’t need full customization.

Option 2: Customize the pytest-html Template (Full Control)

If you want to tweak exactly what’s displayed, you can override the default pytest-html template:

  1. Locate the default template: Find the report.html template in your Python environment’s site-packages/pytest_html/templates directory. Copy this file to your project folder (name it something like custom_report.html).
  2. Edit the template: Open your custom template and find the section that renders failed test details. Look for code like:
    <pre class="code">{{ test.longrepr }}</pre>
    
    Replace that with a version that only shows the error message. For example, if you want just the exception text, you can modify it to extract the relevant part (adjust based on your template version):
    <p>{{ test.longrepr.excepinfo.value if test.longrepr is not none else test.outcome }}</p>
    
  3. Use the custom template: Run pytest with the --template flag:
    pytest --html=test_report.html --self-contained-html --template=custom_report.html
    

Option 3: Use a Pytest Hook to Modify Error Output (Code-Level Control)

Add a hook to your conftest.py file to automatically strip code blocks from failed test reports. This lets you control the output without modifying templates or command lines every time:

import pytest
from _pytest._code.code import ReprExceptionInfo

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # Grab the original report
    outcome = yield
    report = outcome.get_result()
    
    # Only modify failed tests
    if report.failed:
        # Handle exception traceback objects
        if isinstance(report.longrepr, ReprExceptionInfo):
            # Replace the full traceback with just the error message
            report.longrepr = str(report.longrepr.excepinfo.value)
        # Handle string-based tracebacks (fallback)
        elif isinstance(report.longrepr, str):
            # Add cleanup logic here if needed
            pass

Now whenever you run pytest with your normal --html command, failed tests will only show the core error statement.

All these methods work with Python 3.6 and PyCharm. Pick the one that fits your workflow best!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:22:22