【问题标题】:py.test Tracebacks: Highlight my code, fold frames of frameworkpy.test Tracebacks:突出显示我的代码,折叠框架框架
【发布时间】:2014-05-19 17:10:18
【问题描述】:

自从我使用 pytest 以来,我的测试回溯太长了。

Pytests 包括周围的代码行和许多其他信息。

如果回溯行(帧)来自我的代码,我希望看到此信息。但我不想看到它,如果它来自库或框架。

我找不到过滤或折叠框架的方法。

有什么提示吗?

8 年后更新:我认为是时候告别 ASCII 并拥抱 html。使用 html,您可以展开/折叠部分(例如在出色的 django 调试视图中)。

不幸的是,似乎没有 pytest 输出可以为您提供漂亮的界面,例如 sentry。

【问题讨论】:

    标签: python filter pytest stack-trace traceback


    【解决方案1】:

    我有这个猴子补丁我贴在我的conftest.py 当我想这样做时(仅适用于--tb=native):

    这是 Jeremy Allen 的 https://stackoverflow.com/a/24679193/59412 到 pytest 最新版本 6.2.3 的一个端口。这当然不是最干净的东西,但它从 3rd 方依赖项中删除了所有代码 (例如,任何 pip 安装)来自追溯。

    # 5c4048fc-ccf1-44ab-a683-78a29c1a98a6
    import _pytest._code.code
    def should_skip_line(line):
        """
        decide which lines to skip
        """
        return 'site-packages' in line
    
    class PatchedReprEntryNative(_pytest._code.code.ReprEntryNative):
        def __init__(self, tblines):
            self.lines = []
            while len(tblines) > 0:
                # [...yourfilter...]/framework_code.py", line 1, in test_thing
                line = tblines.pop(0)
                if should_skip_line(line):
                    # the line of framework code you don't want to see either...
                    tblines.pop(0)
                else:
                    self.lines.append(line)
    _pytest._code.code.ReprEntryNative = PatchedReprEntryNative
    del _pytest._code.code
    

    【讨论】:

      【解决方案2】:

      恐怕这目前是不可能的,是一个增强请求:https://bitbucket.org/hpk42/pytest/issue/283/traceback-filtering-hook

      您目前唯一的控制权是--tb=short 等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 2011-02-18
        • 2019-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多