python - 避免打印点

标签 python pytest

我使用选项 -q 运行 pytest。

不幸的是,这会打印出很多点。示例:

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

有没有办法避免这一长串的点和“s”字符?

更新

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

最佳答案

冗长选项无法关闭测试结果打印。但是,可以通过多种方式自定义 pytest,包括输出结果。要更改此设置,您将覆盖 pytest_report_teststatus钩子(Hook)。

关闭短字母

创建一个包含以下内容的文件 conftest.py:

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)。代码有点冗长,但可以处理框架中定义的所有标准结果。

关闭详细结果

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

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

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

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 标志时,下面的示例将关闭打印简短和详细的结果:

import pytest

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


def pytest_report_teststatus(report, config):
    category, short, verbose = '', '', ''
    if not 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)

关于python - 避免打印点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374551/

相关文章:

python - 为什么python pandas dataFrame的内存消耗这么大?

python - 为什么在 pytest-django 中使用 ThreadPoolExecutor 时会得到空的 django 查询集?

python - 测试pytest插件时如何获得覆盖率报告?

python - pytest 中的参数化测试对于不同的测试函数具有不同的标记

python - pytest:基于每个模块的选择性日志级别

python - 使用模拟来修补不存在的属性

python - 如何使用try/except/else语法修复 “Unexpected unindent”错误

python - 如何简化 SymPy 中的矩阵表达式?

python - sympy:如何简化三角表达式

python - 在 Django 中连接字段值