Python 使用Form.add_error()时引发ValidationError时出现问题

Python 使用Form.add_error()时引发ValidationError时出现问题,python,django,django-forms,Python,Django,Django Forms,当表单被传递数据时,当调用form.clean()方法时,我正在测试ValidationError。clean()方法计算一个条件,以查看两个字段是否具有相同的值。如果计算结果为true,我想调用Form.add_error()并向其中一个字段添加验证错误 现在,测试没有通过,我得到了以下错误: AssertionError:ValidationError未引发 我不清楚需要重构什么才能通过测试并引发验证错误 forms.py from django import forms from djan

当表单被传递数据时,当调用
form.clean()
方法时,我正在测试
ValidationError
clean()
方法计算一个条件,以查看两个字段是否具有相同的值。如果计算结果为true,我想调用
Form.add_error()
并向其中一个字段添加验证错误

现在,测试没有通过,我得到了以下错误:
AssertionError:ValidationError未引发

我不清楚需要重构什么才能通过测试并引发验证错误

forms.py

from django import forms
from django.core.exceptions import ValidationError

from .validators import validate_name

class NewUserForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField()
    last_name = forms.CharField(
        max_length=20,
        error_messages={'required': "You must provide your last name"},
        validators=[validate_name]
    )
    first_name = forms.CharField(
        max_length=20,
        error_messages={'required': "You must provide your first name"},
        validators=[validate_name]
    )
    reminders = forms.BooleanField(required=True)

    def clean(self):
        cleaned_data = super().clean()
        email = cleaned_data.get("email", None)
        password = cleaned_data.get('password', None)

        if email == password:
            msg = "The password provided cannot be the same as your email"
            self.add_error('password', ValidationError(msg))

from django.test import TestCase
from django.core.exceptions import ValidationError

from ..forms import NewUserForm

class TestNewUserPassword(TestCase):

    @classmethod
    def setUpTestData(cls):
        user_data = {
            "email": "user@email.com",
            "password": "user@email.com",
            "first_name": "firstName",
            "last_name": "lastName"
        }

        cls.test_submitted_form = NewUserForm(user_data)

    def test_password_matches_email_error(self):
        with self.assertRaises(ValidationError):
            # import pdb; pdb.set_trace();
            self.test_submitted_form.is_valid()

测试表单.py

from django import forms
from django.core.exceptions import ValidationError

from .validators import validate_name

class NewUserForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField()
    last_name = forms.CharField(
        max_length=20,
        error_messages={'required': "You must provide your last name"},
        validators=[validate_name]
    )
    first_name = forms.CharField(
        max_length=20,
        error_messages={'required': "You must provide your first name"},
        validators=[validate_name]
    )
    reminders = forms.BooleanField(required=True)

    def clean(self):
        cleaned_data = super().clean()
        email = cleaned_data.get("email", None)
        password = cleaned_data.get('password', None)

        if email == password:
            msg = "The password provided cannot be the same as your email"
            self.add_error('password', ValidationError(msg))

from django.test import TestCase
from django.core.exceptions import ValidationError

from ..forms import NewUserForm

class TestNewUserPassword(TestCase):

    @classmethod
    def setUpTestData(cls):
        user_data = {
            "email": "user@email.com",
            "password": "user@email.com",
            "first_name": "firstName",
            "last_name": "lastName"
        }

        cls.test_submitted_form = NewUserForm(user_data)

    def test_password_matches_email_error(self):
        with self.assertRaises(ValidationError):
            # import pdb; pdb.set_trace();
            self.test_submitted_form.is_valid()