避免打印点

gue*_*tli 1 python pytest

我用option运行pytest -q

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

...................................................................................s...............s...................................ssssss..................................................................................................................................s..............s.........................s..............................................................................................................F....s.s............s.....................s...........................................................................................................................
=================================== FAILURES ===================================
_____________________ TestFoo.test_bar _____________________
Traceback (most recent call last):
  (cut)
Run Code Online (Sandbox Code Playgroud)

有没有办法避免这么长的点和“ s”字符列表?

更新资料

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

hoe*_*ing 5

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

关闭短字母

创建一个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)
Run Code Online (Sandbox Code Playgroud)

现在运行测试将不会打印任何简短的结果字母(.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
...
Run Code Online (Sandbox Code Playgroud)

如果您verbose从上述挂钩隐含中删除了行设置(将其设置为空字符串),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
...
Run Code Online (Sandbox Code Playgroud)
$ 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
...
Run Code Online (Sandbox Code Playgroud)

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

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

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)
Run Code Online (Sandbox Code Playgroud)