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

时间:2014-08-13 13:34:49

标签: python django unit-testing

我跟着Django doc用assertRaisesMessage()编写测试但是问题是在执行测试时引发了异常(因此,测试没有执行)。 请注意,调用的异常是我在模型的方法中自愿引发的异常(不在视图中)。

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

如何使用AssertRaisesMessage()让我的测试执行而不会引发异常? 感谢。

编辑: 在尝试了第一个解决方案后,问题仍然是一样的。一旦我的测试进入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

2 个答案:

答案 0 :(得分:7)

您使用assertRaisesMessage作为您希望失败的代码的上下文管理器:

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

答案 1 :(得分:1)

如果您使用self.client.get,则不会直接获得异常,但您可以检查状态代码。

def test_invertRewardState_view_fails_notmy_reward(self):
    self.client.login(email='gislaine@toto.com', 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='gislaine@toto.com')  # login
    self.assertRaisesMessage(Exception, EXC_NOT_YOURS, invertRewardState, '1')

您可以使用Ned Batchelder建议的上下文管理器表单。