How to print only the summary with pytest?

Viewed 1861

I would like pytest to only print the summary when running my tests. I am using the the -s flag so other output is passed through. Even with --quiet it prints the little green dots (in case of success). I would like to suppress those. It would be also good to have explicit control over what's printed after tests so that I can separate the outputs with whitespace or something, but this is a different question.

I am doing this so that I can check the output of my CLI.

2 Answers

Using my plugin pytest-custom-report you can customize all the glyphs used for report characters. Using an empty string would effectively hide them.

pytest -q --report-passed="" -s

Passing -q to pytest additionally hides the test filenames.

hoefling set me in the right direction. By setting short str, you can customize what's output instead of dots; forwarding the rest of the elements of the tuple while leaving this at an empty string should suffice. I even did some... see for yourself. Using --verbose is not recommended. Could remove more parts, ones that deal with failure cases, but didn't care about that now.

A nice picture of the output. (no rep yet to post it here)

from _pytest.reports import CollectReport, TestReport, Config
from typing import Tuple, Union, Mapping
import py

def pytest_report_teststatus(report: Union[CollectReport, TestReport], config: Config, files_set=set()) \
        -> Tuple[str, str, Union[str, Mapping[str, bool]]]:
    def get_line_delim():
        n = py.io.TerminalWriter().fullwidth // 3
        return f'\n{"*  "*n}\n'
    category, short, verbose = '', '', ''
    if hasattr(report, 'wasxfail'):
        if report.skipped:
            category = 'xfailed'
            verbose = 'xfail'
        elif report.passed:
            category = 'xpassed'
            verbose = ('XPASS', {'yellow': True})
        return (category, short, verbose)
    elif report.when in ('setup', 'teardown'):
        if report.failed:
            category = 'error'
            verbose = 'ERROR'
        elif report.skipped:
            category = 'skipped'
            verbose = 'SKIPPED'
        elif report.when == 'setup' and report.location[0] not in files_set:
            files_set.add(report.location[0]) # needed so that the first test for each file can start out clean
            return (category, get_line_delim(), (verbose, {'red': True}))
        return (category, short, verbose)
    category = report.outcome
    verbose = category.upper()
    return (category, get_line_delim(), (verbose, {'red': True}))
Related