用pytest测试Django干净表单方法的正确方法 表格测试

问题描述

我有一个clean_email方法,其中我将电子邮件字段的输入更改为小写,以避免区分大小写的重复性问题。

我有什么

表格

class UserAdminCreationForm(forms.ModelForm):

    class Meta:
        model = User
        fields = ("username","email","names","last_names","identification_number")
    
    def clean_email(self) -> str:
        """Convert all username to lowercase to avoid duplicity problems with case-sensitive."""
        email = self.cleaned_data["email"]
        return email.lower()

测试

class TestUserCreationForm:
    def test_clean_email(self):
        proto_user = UserFactory.build()  # User created with factory boy.

        form = UserAdminCreationForm(
            {
                "username": proto_user.username,"email": proto_user.email,"names": proto_user.names,"last_names": proto_user.last_names,"identification_number": proto_user.identification_number,}
        )

        assert form.is_valid()
        assert form.clean_email() == form.cleaned_data["email"].lower()

问题

尽管测试通过了,但是当我生成覆盖率报告时,来自return email.lower()方法的声明clean_email显示为未经测试。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)