我刚刚将所有单元测试数据从JSON转换为YAML,现在我的代码中出现异常。更具体地说,这是印刷回溯:
Traceback (most recent call last): File "tests/test_addrtools.py", line 95, in test_validate_correctable_addresses self.assertTrue(self.validator(addr), msg) File "/Users/tomas/Dropbox/Broadnet/broadpy/lib/broadpy/addrtools.py", line 608, in __call__ self.validate(addr) File "/Users/tomas/Dropbox/Broadnet/broadpy/lib/broadpy/addrtools.py", line 692, in validate if self._correction_citytypo(addr): return File "/Users/tomas/Dropbox/Broadnet/broadpy/lib/broadpy/addrtools.py", line 943, in _correction_citytypo ratio = lev_ratio(old_city, city) TypeError: ratio expected two Strings or two Unicodes
现在,第943行的文件“addrtools.py”包含了我的问题的答案。我希望在引发异常的范围内看到old_city
和city
的类型和值。我一直有这样的问题,使用pdb
来检查引发异常的范围内的本地人的快速而无痛的方法将为我节省大量的时间。
我确实尝试了在this question的答案中发布的解决方案,但是验尸功能将我置于 python2.7 / unittest / main.py(231)runTests()这对我帮助不大。我想这是因为异常被从单元测试代码中捕获并重新引发。
答案 0 :(得分:4)
用它包裹:
def debug_on(*exceptions):
if not exceptions:
exceptions = (AssertionError, )
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except exceptions:
pdb.post_mortem(sys.exc_info()[2])
return wrapper
return decorator
示例:
@debug_on(TypeError)
def buggy_function()
....
raise TypeError
答案 1 :(得分:1)
单元测试超集nose有一个选项可以在测试失败时将您降至pdb
,如果您可以使用nose作为测试运行者:
--pdb Drop into debugger on errors
--pdb-failures Drop into debugger on failures