即使引发了ValidationError,表单在单元测试中也是有效的

时间:2017-08-10 13:00:12

标签: python django unit-testing django-forms django-testing

我正在测试处理无效表单数据的视图。在我的测试用例中,我提交了缺少字段的表单,并期望视图通过显示错误消息来处理它。以下是我的表单中clean的相关摘录:

形式:

def clean(self):
    first_name = self.cleaned_data.get('first_name', '')
    last_name = self.cleaned_data.get('last_name', '')

    if not first_name or not last_name:
        print('Form is invalid')
        # This is always executed.
        raise ValidationError('All fields are required.')

    # other stuff
始终会提出

ValidationError,但form.is_valid()仍会返回True

查看:

form = forms.EditProfileForm(request.POST, instance=user)

if form.is_valid():
    print('Form is valid')
    # Update user data. 
    # Always executed even though error was raised.

这导致测试失败,因为我声称表单验证将失败:

测试用例:

def test_invalid_data(self):
    formdata = {
        'first_name': 'Bruno',
        'last_name': '', # Missing data.
        'email': 'bruno@schulz.pl',
        'password': 'metamorphosis'
    }

    request = self.factory.post('/accounts/profile', formdata)
    request.user = self.user
    setup_messages(request)
    response = ProfileView.as_view()(request)

    self.assertEqual(response.status_code, 200)
    self.assertNotEqual(self.user.first_name, formdata['first_name']) # Fails.
    self.assertNotEqual(self.user.last_name, formdata['last_name'])
    self.assertNotEqual(self.user.email, formdata['email'])
    self.assertNotEqual(self.user.username, formdata['email'])

表单验证在实时服务器上正常工作(经过测试"手工和#34;)。

ValidationError评估期间,TestCase似乎被忽略了。

*测试输出:

.Form is invalid
F..Form is valid
.
======================================================================
FAIL: test_invalid_data (.tests.ProfileTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/apps/accounts/tests.py", line 114, in test_invalid_data
    self.assertNotEqual(self.user.first_name, formdata['first_name'])
AssertionError: 'Bruno' == 'Bruno'

2 个答案:

答案 0 :(得分:1)

您尚未显示完整视图,但通常在成功发布后return redirect('/success-url/')。如果您的验证错误被忽略,那么视图将重定向状态代码302,并且您的测试将在早期行self.assertEqual(response.status_code, 200)上失败

您可以使用form = response.context['form']从视图访问该表单。如果您在测试中选中form.is_valid()form.errors,则会看到您的ValidationError未被忽略。

您的问题是您的assertNotEqual支票未测试您认为的内容。验证模型表单时,将修改实例。如果要检查数据库中是否已修改用户,则需要refresh it from the database first

self.user.refresh_from_db()

答案 1 :(得分:-1)

尝试使用self.add_error('last_name', your_validation_error)而不是仅提出错误:

def clean(self):
    first_name = self.cleaned_data.get('first_name', '')
    last_name = self.cleaned_data.get('last_name', '')

    if not first_name:
        print('Form is invalid: missing first name')
        # define but don't raise the error
        missing_first_name = ValidationError('First name field is required.')
        self.add_error('first_name', missing_first_name)
    if not last_name:
        print('Form is invalid: missing last name')
        # define but don't raise error
        missing_last_name = ValidationError('Last name field is required.')
        self.add_error('last_name', missing_last_name)
        # you could also add a string instead of an Error, but a ValidationError is recommended:
        # self.add_error('last_name', 'the last name field is missing')

# other stuff

现在form.is_valid()将返回False。

This SO answer让我找到了解决方案,here are the add_error docs。您应该使用self.add_error将错误添加到表单中,而不是引发验证错误。

(我确实找到this example in the docs,其中错误被引发而不是传递给self.add_error,这可能是您混淆的原因,但那是为了清理特定字段。它不是{ {1}}方法。我假设您可以捕获该错误,然后将其添加到form.clean() ...但这是一个不同的问题需要解决。重点是,我没有在文档中看到示例self.add_error应该引发ValidationError。它应该将它添加到表单中。)