Django在上下文中检查错误消息

时间:2012-10-29 21:01:19

标签: django django-testing

我有一个简单的测试:

def test_project_info_form_post_submission(self):
    """
    Test if project info form can be submitted via post.
    """
    # set up our POST data
    post_data = {
        'zipcode': '90210',
        'module': self.module1.pk,
        'model': self.model1.pk,
        'orientation': 1,
        'tilt': 1,
        'rails_direction': 1,
    }
    ...
    response = self.client.post(reverse(url), post_data)
    self.assertEqual(response.status_code, 302)

    # test empty form
    response = self.client.post(reverse(url))
    self.assertEqual(response.status_code, 200)
    #! test for general form error message 

    # now test invalid responses
    post_data['zipcode'] = 'abcdefg'
    response = self.client.post(reverse(url), post_data)
    self.assertEqual(response.status_code, 200)
    #! test for specific error message associated with zipcode

所以我遇到麻烦的台词标有shebangs。我知道我应该在上下文变量中有消息,但似乎无法找出正确使用的消息。

1 个答案:

答案 0 :(得分:1)

您可以使用assertContains测试模板是否在TestCase中包含您的消息。