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

如何在保留实时进度更新的同时调整pytest输出分隔符?

修改pytest分隔符样式并保留实时进度的方法
  • 自定义pytest插件调整分隔符
    在项目根目录创建conftest.py文件,通过重写终端报告器的分隔符生成方法,自定义分隔符的长度和样式:

    import pytest
    from _pytest.terminal import TerminalReporter
    
    def pytest_configure(config):
        reporter = config.pluginmanager.get_plugin("terminalreporter")
        if isinstance(reporter, TerminalReporter):
            def custom_sep(title, sep_char="-", length=40):
                return f"{sep_char*length} {title} {sep_char*length}\n"
            reporter._sep = custom_sep
    

    这段代码会将原本占满终端宽度的分隔符替换为固定长度的样式,同时完全保留实时测试进度的显示。

  • 通过命令行参数简化报告
    使用--tb参数调整回溯报告格式,缩短分隔符长度:

    pytest --tb=short
    

    该参数会简化失败报告的结构,减少冗余的宽分隔符,且不影响实时进度更新。

  • 修改测试用例标题分隔线
    同样在conftest.py中添加代码,单独调整测试用例名称前后的分隔线长度:

    import pytest
    from _pytest.terminal import TerminalReporter
    
    def pytest_configure(config):
        reporter = config.pluginmanager.get_plugin("terminalreporter")
        if isinstance(reporter, TerminalReporter):
            reporter._testsep = "-" * 20
    

    这样测试用例标题的分隔线会缩短为指定长度,避免占满终端宽度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 16:39:51