python - Pytest:自定义简短的测试摘要信息,删除文件路径
问题描述
我试图从pytest -tb=no
短输出中获得更有用的输出。我有存储在 JSON 文件中的集成测试,所以输出看起来都非常相似。
tests/test_dit_cli.py .......F............................. [ 29%]
...F...F.FF........F............................F...FFFFFFF [ 75%]
FFF.F..................F.....FF [100%]
===================== short test summary info =====================
FAILED tests/test_dit_cli.py::test_dits[dit_json7] - assert "Lin...
FAILED tests/test_dit_cli.py::test_dits[dit_json40] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json44] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json46] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json47] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json56] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json85] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json89] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json90] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json91] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json92] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json93] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json94] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json95] - AssertionE...
FAILED tests/test_dit_cli.py::test_dits[dit_json96] - assert 'Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json97] - assert 'Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json98] - assert "Li...
FAILED tests/test_dit_cli.py::test_dits[dit_json100] - Assertion...
FAILED tests/test_dit_cli.py::test_dits[dit_json119] - assert "L...
FAILED tests/test_dit_cli.py::test_dits[dit_json125] - Assertion...
FAILED tests/test_dit_cli.py::test_dits[dit_json126] - Assertion...
================= 21 failed, 106 passed in 2.94s ==================
看到同样tests/test_dit_cli.py::test_dits[dit_json126]
的 20 次并不能帮助我了解项目中出了什么问题,所以我通常一次只修复一个测试错误。每个测试条目都有关于正在运行的测试类型和预期结果的额外信息,但我不知道如何将这些信息输入 pytest。我希望有这样的事情:
===================== short test summary info =====================
FAILED [func, vanilla Python] - assert "Li...
FAILED [Thing, value assignment] - assert "Li...
FAILED [TypeMismatch, String var assigned to List] - assert "Lin...
ids
通过在parametrize
调用中提供值,我实际上接近了这一点。
def pytest_generate_tests(metafunc: Metafunc):
for fixture in metafunc.fixturenames:
if fixture == "dit_json":
test_dicts = list(load_from_json())
titles = [test_dict["title"] for test_dict in test_dicts]
metafunc.parametrize(argnames=fixture, argvalues=test_dicts, ids=titles)
FAILED tests/test_dit_cli.py::test_dits[TypeMismatch, List var assigned to String]
FAILED tests/test_dit_cli.py::test_dits[import, anon import referenced in list assignment]
所以,我真的很接近,我只想删除文件路径,以便行更短。有没有办法改变它认为测试所在的文件路径?或者一个可以让我任意修改摘要输出的钩子?我尝试修改pytest_collection_modifyitems
和更改item.fspath
,但它没有改变输出中的任何内容。我已经看到了修改有关输出的许多其他内容的方法,但没有具体涉及该文件路径。
解决方案
如果您只想缩短简短摘要信息中的节点ID,则可以覆盖对象的nodeid
属性report
。一个简单的例子:
def pytest_runtest_logreport(report):
report.nodeid = "..." + report.nodeid[-10:]
放置在您的conftest.py
, 中会将每个 nodeid 截断为其最后十个字符:
=========================== short test summary info ===========================
FAILED ...st_spam[0] - assert False
FAILED ...st_spam[1] - assert False
FAILED ...st_spam[2] - assert False
FAILED ...st_spam[3] - assert False
FAILED ...st_spam[4] - assert False
FAILED ...:test_eggs - assert False
如果您想要一个完全自定义的简短测试摘要行,您需要TerminalReporter
在测试运行中尽早实现自定义并替换原版。示例存根:
import pytest
from _pytest.terminal import TerminalReporter
class MyReporter(TerminalReporter):
def short_test_summary(self):
# your own impl goes here, for example:
self.write_sep("=", "my own short summary info")
failed = self.stats.get("failed", [])
for rep in failed:
self.write_line(f"failed test {rep.nodeid}")
@pytest.mark.trylast
def pytest_configure(config):
vanilla_reporter = config.pluginmanager.getplugin("terminalreporter")
my_reporter = MyReporter(config)
config.pluginmanager.unregister(vanilla_reporter)
config.pluginmanager.register(my_reporter, "terminalreporter")
这将产生一个摘要部分,如
========================== short test summary info ===========================
failed test tests/test_spam.py::test_spam[0]
failed test tests/test_spam.py::test_spam[1]
failed test tests/test_spam.py::test_spam[2]
failed test tests/test_spam.py::test_spam[3]
failed test tests/test_spam.py::test_spam[4]
failed test tests/test_spam.py::test_eggs
请注意,上面的 implMyReporter.short_test_summary()
并不完整,仅用于演示目的!如需参考,请查看pytest
impl。
推荐阅读
- sql-server - 有没有办法通过逐行查找的 sql 表来循环计算?
- python - 在 SAML 中,当要对用户/密码对进行身份验证时,用户/密码对位于 AuthnRequest 中的什么位置?
- azure - 需要帮助在 Azure VM 上安装 SSL.com 证书
- android - 如何在 kotlin 中以编程方式更改文本颜色
- qt - 组织孩子的 Qt Quick2 自定义控件
- javascript - Typescript - 如何使用动态对象数组中的键动态键入对象
- typescript - 打字稿:无法推断类实例方法的已知返回类型
- c# - 尝试从浏览器 sessionStorage 中“getItem()”时,Blazor 失败
- r - 在 R 中创建一个广义函数
- javascript - 反应等待一个循环(和 setState)在另一个循环之前完成