我用选项-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 挂钩.
关掉短信
使用以下内容创建文件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
...
如果从上面的钩子impl中删除设置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)
来源:https://www./content-1-482351.html
|