表单验证的测试用例未在djang中运行

2024-09-24 06:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始学习django框架,因为我已经创建了验证相互依赖字段的表单。现在我已经为它编写了一个测试用例,但是我的控制台显示有0个测试用例运行,我不明白它为什么不运行

下面是我的表单.py在

class SearchForm(forms.ModelForm):
    fromDate=forms.DateField()
    toDate=forms.DateField(required=False)
    fromLocation=forms.CharField()
    toLocation=forms.CharField()
    def clean(self):
        """verify from and to date and location"""
        cleanedData=super().clean()
        fLocation=cleanedData.get('fromLocation')
        tLocation=cleanedData.get('toLocation')
        self.validateLocation(fLocation,tLocation)
        self.validateDates(self.fromDate,self.toDate)

    def validateLocation(self,fLocation,tLocation):
        if fLocation==tLocation:
            raise forms.ValidationError(_("from and to location can not be same"),code="invalid location")

    def validateDates(self,fromDate,toDate):
        if toDate is not None:
            if toDate <= fromDate:
                raise forms.ValidationError(_("from date should be less than to date"),code="invalid date")

还有我的测试.py在

^{pr2}$

请告诉我我哪里出错了。仅供参考,我试图覆盖干净的表单方法,但没有运气


Tags: andtofromself表单dateifdef