How can I show verbose py.test diffs without verbose test progress?

4.5k views Asked by At

py.test's --verbose option is required to show full diffs on assertion failures, but this also displays the full name of each test during execution (which is noisy).

I'd like full diffs to show when an assertion fails, but I only want single .'s to appear when the tests are running. Is there a way to do this?

3

There are 3 answers

2
bereal On BEST ANSWER

Unfortunately, there seems to be no configuration or command line flag for that, since that's hard-coded deep inside pytest: when you define --verbose, you get the whole package. However, I've managed to come up with this hackish hack. Put the following function into your 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 

This is essentially a monkey-patching, relying on pytest internals, not guaranteed to be compatible with the future versions and ugly as sin. You can also make this patch conditional based on some other custom configuration of command-line argument.

0
binaryfunt On

A workaround is to set CI=true environment variable. E.g.

$ CI=true pytest --quiet

Pytest seems to be configured to always output full diff in CI.

0
andras.tim On

Based on @bereal's answer

(this is good, but should follow up some pytest changes)

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