python – doctests中的链式异常
作者:互联网
我编写了一个用于测试的assert_raised上下文管理器,它检查是否引发了预期的异常,如果没有引发AssertionError.我也写了一个doctest来测试这个,但doctest一直在失败,我不完全确定为什么.这是doctest:
>>> for e in [TypeError, ValueError, KeyError]:
... with assert_raised(TypeError, ValueError):
... print('Raising {}...'.format(e.__name__))
... raise e
Raising TypeError...
Raising ValueError...
Raising KeyError...
Traceback (most recent call last):
...
AssertionError: Got 'KeyError', expected 'TypeError, ValueError'
提出的实际例外是:
Traceback (most recent call last):
File "<doctest dtlibs.mock.assert_raised[3]>", line 4, in <module>
raise e
KeyError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python32\lib\doctest.py", line 1253, in __run
compileflags, 1), test.globs)
File "<doctest dtlibs.mock.assert_raised[3]>", line 4, in <module>
raise e
File "G:\Projects\Programming\dt-tools\dtlibs\dtlibs\mock.py", line 274, in __exit__
raise self._exception(exc_type.__name__)
AssertionError: Got 'KeyError', expected 'TypeError, ValueError'
我不认为实现很重要,但是这里是因为我在那里做错了(没有doctest):
class assert_raised:
def __init__(self, *exceptions):
self.exceptions = exceptions
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
raise self._exception('None')
elif self.exceptions and exc_type not in self.exceptions:
raise self._exception(exc_type.__name__)
return True
def _exception(self, got):
if len(self.exceptions) == 0:
expected = 'an exception'
else:
expected = ', '.join(e.__name__ for e in self.exceptions)
msg = "Got '{}', expected '{}'".format(got, expected)
return AssertionError(msg)
解决方法:
Doctests有处理提出异常的特殊代码.因此,它将识别输出何时是异常.为了识别这一点,预期的输出必须以Traceback一词开头.您的预期输出不会以此开头,因此,doctest不会将输出识别为期望异常,因此,它不会期望异常,因此当异常到来时,它将失败.
您可以通过三种方式解决此问题:
>摆脱Raising XXXError …输出的一部分. [懒]
>为doctest实现特殊的输出过滤器,使doctest能够忽略Raising XXXError …-部分[复杂]
>停止将doctests用于除测试文档之外的其他内容. [正确]
上面的代码显然不是如何使用这个上下文管理器的示例代码,它是测试上下文管理器工作的代码.这样的测试永远不应该是doctests. Doctests是痛苦和有限的,应该只用于测试文档.
标签:python,exception,python-3-x,doctest 来源: https://codeday.me/bug/20190626/1291101.html