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

时间:2015-06-19 12:57:28

标签: python pytest

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

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

2 个答案:

答案 0 :(得分:2)

不幸的是,似乎没有配置或命令行标志,因为那是硬编码deep inside pytest:当您定义--verbose时,您将获得整个包。但是,我已经设法提出这个hackish hack。将以下函数放入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 

这实际上是一个猴子修补,依赖pytest内部,不保证与未来的版本兼容,丑陋的罪恶。您还可以根据命令行参数的其他自定义配置使此修补程序成为条件。

答案 1 :(得分:0)

基于@bereal的答案

(这很好,但是应该进行一些pytest更改)

def pytest_configure(config):
    terminal = config.pluginmanager.getplugin('terminal')

    class QuietReporter(terminal.TerminalReporter):
        @property
        def verbosity(self):
            return 0

        @property
        def showlongtestinfo(self):
            return False

        @property
        def showfspath(self):
            return False

    terminal.TerminalReporter = QuietReporter
相关问题