Avoid printing of dots

谁说我不能喝 提交于 2019-11-28 10:21:41

问题


I run pytest with option -q.

Unfortunately this prints out a lot of dots. Example:

...................................................................................s...............s...................................ssssss..................................................................................................................................s..............s.........................s..............................................................................................................F....s.s............s.....................s...........................................................................................................................
=================================== FAILURES ===================================
_____________________ TestFoo.test_bar _____________________
Traceback (most recent call last):
  (cut)

Is there a way to avoid this long list of dots and "s" characters?

Update

There is a valid answer. But somehow it is too long for me. I use this workaround now:I added this to the script which calls pytest: pytest -q | perl -pe 's/^[.sxFE]{20,}$//g'


回答1:


The verbosity options can't turn off the test outcome printing. However, pytest can be customized in many ways, including the outcome printing. To change this, you would override the pytest_report_teststatus hook.

turn off short letters

Create a file conftest.py with the following content:

import pytest

def pytest_report_teststatus(report):
    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'
        return (category, short, verbose)
    category = report.outcome
    verbose = category.upper()
    return (category, short, verbose)

Now running the tests will not print any short outcome letters (.sxFE). The code is a bit verbose, but handles all the standard outcomes defined in the framework.

turn off verbose outcomes

When running in verbose mode, pytest prints the outcome along with the test case name:

$ pytest -sv
=================================== test session starts ===================================
...
test_spam.py::test_spam PASSED
test_spam.py::test_eggs FAILED
test_spam.py::test_bacon SKIPPED
test_spam.py::test_foo xfail
...

If you remove the lines setting verbose from the above hook impl (leaving it set to an empty string), pytest will also stop printing outcomes in verbose mode:

import pytest

def pytest_report_teststatus(report):
    category, short, verbose = '', '', ''
    if hasattr(report, 'wasxfail'):
        if report.skipped:
            category = 'xfailed'
        elif report.passed:
            category = 'xpassed'
        return (category, short, verbose)
    elif report.when in ('setup', 'teardown'):
        if report.failed:
            category = 'error'
        elif report.skipped:
            category = 'skipped'
        return (category, short, verbose)
    category = report.outcome
    return (category, short, verbose)
$ pytest -sv
=================================== test session starts ===================================
...
test_spam.py::test_spam
test_spam.py::test_eggs
test_spam.py::test_bacon
test_spam.py::test_foo
...

introducing custom reporting mode via command line switch

The below example will turn off printing both short and verbose outcomes when --silent flag is passed from command line:

import pytest

def pytest_addoption(parser):
    parser.addoption('--silent', action='store_true', default=False)


def pytest_report_teststatus(report):
    category, short, verbose = '', '', ''
    if not pytest.config.getoption('--silent'):
        return None

    if hasattr(report, 'wasxfail'):
        if report.skipped:
            category = 'xfailed'
        elif report.passed:
            category = 'xpassed'
        return (category, short, verbose)
    elif report.when in ('setup', 'teardown'):
        if report.failed:
            category = 'error'
        elif report.skipped:
            category = 'skipped'
        return (category, short, verbose)
    category = report.outcome
    return (category, short, verbose)


来源:https://stackoverflow.com/questions/53374551/avoid-printing-of-dots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!