如何在 Django 测试中使用 AssertRaisesMessage()

问题描述 投票:0回答:2

我按照 Django doc 使用assertRaisesMessage() 编写测试,但问题是在执行测试时引发异常本身(因此,测试未执行)。 请注意,调用的异常是我在模型的方法中自愿引发的异常(不在视图中)。

class MyTestCase(TestCase):
    def test_invertRewardState_view_fails_notmy_reward(self):
        self.client.login(email='[email protected]', password='azerty')
        resp = self.client.get(reverse(invertRewardState, args=(1,)))
        self.assertRaisesMessage(
            expected_exception=Exception,
            expected_message=EXC_NOT_YOURS,
            callable_obj=resp)

我应该如何使用 AssertRaisesMessage() 来执行我的测试而不引发异常? 谢谢。

编辑: 尝试 falsetru 第一个解决方案后,问题仍然相同。一旦我的测试进入

resp = ...
部分,视图就会被调用,然后相关的模型方法就会被调用并引发异常。 完整的堆栈跟踪:

Traceback (most recent call last):
  File "/Users/walt/Code/hellodjango/clientizr/tests.py", line 338, in test_invertRewardState_view_fails_notmy_reward
    resp = self.client.get(reverse(invertRewardState, args=('1',)))
  File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/test/client.py", line 473, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/test/client.py", line 280, in get
    return self.request(**r)
  File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/test/client.py", line 444, in request
    six.reraise(*exc_info)
  File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/walt/Code/hellodjango/clientizr/views.py", line 234, in invertRewardState
    reward.invert_reward_state(request.user)
  File "/Users/walt/Code/hellodjango/clientizr/models.py", line 606, in invert_reward_state
    self.throw_error_if_not_owner_reward(cur_user)
  File "/Users/walt/Code/hellodjango/clientizr/models.py", line 587, in throw_error_if_not_owner_reward
    raise Exception(EXC_NOT_YOURS)
Exception: Cet objet n'est pas le v\xf4tre
python django unit-testing
2个回答
14
投票

您使用

assertRaisesMessage
作为您预计会失败的代码的上下文管理器:

class MyTestCase(TestCase):
    def test_invertRewardState_view_fails_notmy_reward(self):
        self.client.login(email='[email protected]', password='azerty')
        url = reverse(invertRewardState, args=(1,))
        with self.assertRaisesMessage(Exception, EXC_NOT_YOURS):
            self.client.get(url)

1
投票

如果使用

self.client.get
,不会直接得到异常,但可以查看状态码。

def test_invertRewardState_view_fails_notmy_reward(self):
    self.client.login(email='[email protected]', password='azerty')
    resp = self.client.get(reverse(invertRewardState, args=('1',)))
    self.assertEqual(resp.status_code, 500)
    self.assertIn(EXC_NOT_YOURS in resp.content)

如果想获得异常,直接调用视图即可。

def test_invertRewardState_view_fails_notmy_reward(self):
    request = HttpRequest()
    request.user = User.objects.create(email='[email protected]')  # login
    self.assertRaisesMessage(Exception, EXC_NOT_YOURS, invertRewardState, '1')

您可以按照 Ned Batchelder 的建议使用上下文管理器形式。

© www.soinside.com 2019 - 2024. All rights reserved.