首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Pytest:自定义短测试摘要信息,删除filepath

Pytest:自定义短测试摘要信息,删除filepath
EN

Stack Overflow用户
提问于 2020-12-31 23:02:24
回答 2查看 3.7K关注 0票数 5

我试图从pytest -tb=no短输出中获得更多有用的输出。我在JSON文件中存储了集成测试,因此输出看起来都非常相似。

代码语言:javascript
运行
复制
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。我希望有这样的事情:

代码语言:javascript
运行
复制
===================== 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提供了一个值,从而接近了这一点。

代码语言:javascript
运行
复制
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)
代码语言:javascript
运行
复制
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,但是它没有改变输出中的任何内容。我看到了修改输出的许多其他东西的方法,但没有具体地修改那个文件路径。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-02 16:37:40

如果您只想缩短简短摘要信息中的节点,可以覆盖report对象的report属性。一个简单的例子:

代码语言:javascript
运行
复制
def pytest_runtest_logreport(report):
    report.nodeid = "..." + report.nodeid[-10:]

放置在您的conftest.py中,将将每个节点截断到其最后的十个字符:

代码语言:javascript
运行
复制
=========================== 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,并在测试运行中尽早替换一个普通的测试摘要。示例存根:

代码语言:javascript
运行
复制
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")

这将产生一个摘要部分,如

代码语言:javascript
运行
复制
========================== 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

请注意,上述的MyReporter.short_test_summary()的推动是不完整的,只是为了演示的目的!作为参考,请查看pytest impl

票数 7
EN

Stack Overflow用户

发布于 2022-08-30 06:22:37

@IsaiahShiner

至于@final问题,您可以这样做:

代码语言:javascript
运行
复制
from _pytest.terminal import TerminalReporter


MyReporter = type('MyReporter', (TerminalReporter, ), {})

def __init__(self, config, file=None):
  super(MyReporter, self).__init__(config, file)

MyReporter.__init__ = __init__

看起来不太好,但很管用。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65526149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档