避免打印点 [英] Avoid printing of dots

查看:39
本文介绍了避免打印点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用选项 -q 运行 pytest.

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)

有没有办法避免这个长长的点和s"字符列表?

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

更新

有一个有效的答案.但不知何故,这对我来说太长了.我现在使用此解决方法:我将其添加到调用 pytest 的脚本中: pytest -q |perl -pe 's/^[.sxFE]{20,}$//g'

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'

推荐答案

详细选项无法关闭测试结果打印.但是,pytest 可以通过多种方式进行自定义,包括结果打印.要更改此设置,您需要覆盖 pytest_report_teststatus 钩子.

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.

创建一个文件conftest.py,内容如下:

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)

现在运行测试不会打印任何简短的结果字母 (.sxFE).代码有点冗长,但处理了框架中定义的所有标准结果.

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.

在详细模式下运行时,pytest 打印结果和测试用例名称:

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
...

如果您从上面的钩子实现中删除设置 verbose 的行(将其设置为空字符串),pytest 也将停止在详细模式下打印结果:

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
...

通过命令行开关引入自定义报告模式

当从命令行传递 --silent 标志时,以下示例将关闭打印简短和详细的结果:

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)

这篇关于避免打印点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆