gpt4 book ai didi

python - py.test Tracebacks : Highlight my code, 框架的折叠框架

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:34 26 4
gpt4 key购买 nike

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

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

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

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

有什么提示吗?

更新,8 年后:我认为是时候告别 ASCII 并拥抱 html 了。使用 html,您可以展开/折叠部分(就像在很棒的 django 调试 View 中一样)。

不幸的是,似乎没有 pytest 输出可以为您提供像哨兵那样的良好界面。

最佳答案

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

这是 https://stackoverflow.com/a/24679193/59412 的端口由 Jeremy Allen 升级到最新版本的 pytest,6.2.3。这当然不是最干净的东西,但它删除了第 3 方依赖项中的所有代码(例如,安装的任何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

关于python - py.test Tracebacks : Highlight my code, 框架的折叠框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com