如何在没有详细测试进度的情况下显示详细的py.test差异?

Jac*_*ing 10 python pytest

py.test--verbose在断言失败时需要显示完全差异的选项,但这也会在执行期间显示每个测试的全名(这是有噪声的).

我想在断言失败时显示完全差异,但我只想.在测试运行时出现单个.有没有办法做到这一点?

ber*_*eal 3

不幸的是,似乎没有配置或命令行标志,因为它是硬编码在 pytest 内部的深处:当您定义时--verbose,您将获得整个包。然而,我设法想出了这个黑客技巧。将以下函数放入您的conftest.py

def pytest_configure(config):
    terminal = config.pluginmanager.getplugin('terminal')
    BaseReporter = terminal.TerminalReporter
    class QuietReporter(BaseReporter):
        def __init__(self, *args, **kwargs):
            BaseReporter.__init__(self, *args, **kwargs)
            self.verbosity = 0
            self.showlongtestinfo = self.showfspath = False

    terminal.TerminalReporter = QuietReporter 
Run Code Online (Sandbox Code Playgroud)

这本质上是一个猴子补丁,依赖于 pytest 内部结构,不能保证与未来版本兼容,并且丑陋如罪过。您还可以根据命令行参数的其他一些自定义配置来使此补丁成为有条件的。