【发布时间】:2017-07-27 09:56:09
【问题描述】:
我在views.py中写了一段代码
def fun():
try:
--Do some operation--
except OSError:
--Do something else--
我已经编写了一个测试用例来涵盖代码的全部功能。为了测试“除外”部分,我编写了以下代码,它将引发“OSError”,
with pytest.raises(OSError):
response = client.post(reverse('my_views_funurl'), follow=True)
但是,我收到了这个错误
response = client.post(reverse('my_views_funurl'), follow=True)
E Failed: DID NOT RAISE
如何引发“OSError”以覆盖测试用例中的异常部分。 顺便说一句,我正在使用 django-framework
【问题讨论】:
-
您可以对不存在的文件进行操作,这会给您带来 oserror ex: os.remove('file') but file doesn't exist.
-
您模拟应该引发异常并将异常作为副作用的函数。 docs.python.org/3/library/unittest.mock.html#quick-guide
标签: python django pytest django-tests