我使用选项-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'
答案 0 :(得分:3)
详细信息选项无法关闭测试结果打印。但是,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)
现在运行测试将不会打印任何简短的结果字母(.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
...
如果您从上述钩子印象中删除设置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):
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)
答案 1 :(得分:1)
使用我的插件pytest-custom-report
,您可以通过在报告中使用空字符串标记来轻松隐藏这些点。
pip install pytest-custom-report
pytest -q --report-passed=""