gue*_*tli 6 python unit-testing
py.test stacktraces目前看起来像这样:
Traceback (most recent call last):
File "/home/foo_tbz_di476/src/djangotools/djangotools/tests/ReadonlyModelTestCommon.py", line 788, in test_stale_or_missing_content_types
self.assertEqual([], errors, 'Stale/Missing ContentTypes: %s' % '\n'.join(errors))
File "/usr/lib64/python2.7/unittest/case.py", line 511, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib64/python2.7/unittest/case.py", line 740, in assertListEqual
self.assertSequenceEqual(list1, list2, msg, seq_type=list)
File "/usr/lib64/python2.7/unittest/case.py", line 722, in assertSequenceEqual
self.fail(msg)
File "/usr/lib64/python2.7/unittest/case.py", line 408, in fail
raise self.failureException(msg)
Run Code Online (Sandbox Code Playgroud)
如果输出会跳过unittest模块中的线条,对我的人眼来说会容易得多.
例:
Traceback (most recent call last):
File "/home/foo_tbz_di476/src/djangotools/djangotools/tests/ReadonlyModelTestCommon.py", line 788, in test_stale_or_missing_content_types
self.assertEqual([], errors, 'Stale/Missing ContentTypes: %s' % '\n'.join(errors))
Run Code Online (Sandbox Code Playgroud)
我尝试过选项,--tb=short但这不会这样做.
更新
没有unix管(类似py.test ...| grep)的溶液是优选的.
看起来你正在调用pytest,如:
py.test --tb=native
Run Code Online (Sandbox Code Playgroud)
此表单将输出从traceback.format_exception派生的python stdlib stacktrace
使用pytest,您可以将conftest.py文件添加到项目中.在这里,您可以添加任何代码来修改pytest行为.
谨防!在以下两种方法中都使用猴子修补,人们可能会认为是邪恶的.
这是最简单的方法,但如果您要搜索的字符串出现在您不想隐藏的行中,则可能会出现问题.
这种方法修补py包中的ReprEntryNative类,这是pytest的依赖.
将以下代码放入conftest.py中
import py
def test_skip_line(line):
"""decide which lines to skip, the code below will also skip the next line if this returns true"""
return 'unittest' in line
class PatchedReprEntryNative(py._code.code.ReprEntryNative):
def __init__(self, tblines):
self.lines = []
while len(tblines) > 0:
line = tblines.pop(0)
if test_skip_line(line):
# skip this line and the next
tblines.pop(0)
else:
self.lines.append(line)
py._code.code.ReprEntryNative = PatchedReprEntryNative
Run Code Online (Sandbox Code Playgroud)
如果字符串匹配对你来说不够真实,我们可以在它被转储到字符串之前检查回溯,并且只输出不是来自一组模块的帧.
这种方法修补了traceback.extract_tb函数,它可能会杀死小狗.
将以下代码放入conftest.py中
import inspect
import linecache
import traceback
import unittest.case
import sys
SKIPPED_MODULES = [
unittest.case
]
def test_skip_frame(frame):
module = inspect.getmodule(frame)
return module in SKIPPED_MODULES
def tb_skipper(tb):
tbnext = tb.tb_next
while tbnext is not None:
if test_skip_frame(tbnext.tb_frame):
tbnext = tbnext.tb_next
else:
yield tbnext
yield None
def new_extract_tb(tb, limit = None):
if limit is None:
if hasattr(sys, 'tracebacklimit'):
limit = sys.tracebacklimit
list = []
n = 0
new_tb_order = tb_skipper(tb) # <-- this line added
while tb is not None and (limit is None or n < limit):
f = tb.tb_frame
lineno = tb.tb_lineno
co = f.f_code
filename = co.co_filename
name = co.co_name
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
if line: line = line.strip()
else: line = None
list.append((filename, lineno, name, line))
tb = next(new_tb_order) # <-- this line modified, was tb = tb.tb_next
n = n+1
return list
traceback.extract_tb = new_extract_tb
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2706 次 |
| 最近记录: |