python - 希望在 pytest 输出中查看取消选择的测试列表及其节点 ID
问题描述
是否有一个选项可以在 cli 输出中列出取消选择的测试以及触发它们取消选择的标记?
我知道在具有许多测试的套件中,这不是默认设置,但在 api 测试之类的测试可能会受到更多限制的情况下,这将是一个有用的选项。
数字摘要
collected 21 items / 16 deselected / 5 selected
在尝试组织标记并查看 ci 构建中发生的情况时很有帮助,但还不够。
解决方案
pytest
有一个pytest_deselected
用于访问取消选择的测试的钩子规范。示例:将此代码添加到conftest.py
您的测试根目录中:
def pytest_deselected(items):
if not items:
return
config = items[0].session.config
reporter = config.pluginmanager.getplugin("terminalreporter")
reporter.ensure_newline()
for item in items:
reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True)
现在运行测试将为您提供类似于以下内容的输出:
$ pytest -vv
...
plugins: cov-2.8.1, asyncio-0.10.0
collecting ...
deselected: test_spam.py::test_spam
deselected: test_spam.py::test_bacon
deselected: test_spam.py::test_ham
collected 4 items / 3 deselected / 1 selected
...
如果您想要其他格式的报告,只需将取消选择的项目存储在配置中并将它们用于其他地方所需的输出,例如pytest_terminal_summary
:
# conftest.py
import os
def pytest_deselected(items):
if not items:
return
config = items[0].session.config
config.deselected = items
def pytest_terminal_summary(terminalreporter, exitstatus, config):
reports = terminalreporter.getreports('')
content = os.linesep.join(text for report in reports for secname, text in report.sections)
deselected = getattr(config, "deselected", [])
if deselected:
terminalreporter.ensure_newline()
terminalreporter.section('Deselected tests', sep='-', yellow=True, bold=True)
content = os.linesep.join(item.nodeid for item in deselected)
terminalreporter.line(content)
给出输出:
$ pytest -vv
...
plugins: cov-2.8.1, asyncio-0.10.0
collected 4 items / 3 deselected / 1 selected
...
---------------------------------------- Deselected tests -----------------------------------------
test_spam.py::test_spam
test_spam.py::test_bacon
test_spam.py::test_ham
================================= 1 passed, 3 deselected in 0.01s =================================
推荐阅读
- android - 无法在android studio中打开运行窗口
- salesforce - CPQ报价线显示多次
- inkcanvas - 手写功能就像 fiinote 手写一样
- android - 在本机反应中发布后崩溃的应用程序?
- flutter - 如何限制 TextSpan 小部件的文本长度
- javascript - 单击“Enter”按钮时提交表单,并进行验证
- flutter - 如何在不改变屏幕/页面的情况下传递数据?
- kubernetes - 是否可以在容器内的 env 中将 application.properties 的内容作为键/值对公开?
- javascript - 使用 node.js 处理来自 aws s3 getobject 的数百万条记录并通过分页将记录返回到前端的最佳方法
- python - Django 连接错误(Oracle 11g 数据库)