gue*_*tli 2 python filter stack-trace traceback pytest
自从我使用 pytest 以来,我的测试的回溯太长了。
Pytests 包括周围的代码行和许多其他信息。
如果回溯线(框架)来自我的代码,我希望看到此信息。但我不想看到它,如果它来自库或框架。
我找不到过滤或折叠框架的方法。
有什么提示吗?
8 年后更新:我认为是时候告别 ASCII 并拥抱 html 了。使用 html,您可以展开/折叠部分(就像在很棒的 django 调试视图中一样)。
不幸的是,似乎没有 pytest 输出可以为您提供像哨兵那样的良好界面。
conftest.py
当我想这样做时,我有这个猴子补丁,我会贴在我的身上(仅适用于--tb=native):
这是 Jeremy Allen 的/sf/answers/1727543541/到最新版本 pytest 6.2.3 的端口。pip这当然不是最干净的东西,但它从回溯中删除了来自第 3 方依赖项(例如安装的任何内容)的所有代码。
# 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
Run Code Online (Sandbox Code Playgroud)