Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试表单';Django中单元测试驱动程序中的验证逻辑?_Django_Django Forms_Django Testing - Fatal编程技术网

如何测试表单';Django中单元测试驱动程序中的验证逻辑?

如何测试表单';Django中单元测试驱动程序中的验证逻辑?,django,django-forms,django-testing,Django,Django Forms,Django Testing,我想测试表单验证逻辑的有效部分。在我的测试驱动程序中,我有: test_animal = Animal(name="cat", number_paws="4") test_animal_form = AnimalForm(instance=test_animal) assertEqual(test_animal_form.is_valid(), True) 试验动物=动物(name=“cat”,number\u paws=“4”) 测试动物形态=动物形态(实例=测试动物) assertE

我想测试表单验证逻辑的有效部分。在我的测试驱动程序中,我有:

test_animal = Animal(name="cat", number_paws="4") test_animal_form = AnimalForm(instance=test_animal) assertEqual(test_animal_form.is_valid(), True) 试验动物=动物(name=“cat”,number\u paws=“4”) 测试动物形态=动物形态(实例=测试动物) assertEqual(测试动物形式是否有效(),真)
断言失败了,但据我所见,表单中不应该有任何错误。我在表单中没有看到任何验证错误。如果加载到表单中的测试动物实例应进行验证,则此项是否应作为测试用例使用?

您看到验证错误的原因是验证中未使用实例数据,您必须指定发送到表单的数据

test_animal = Animal(name="cat", number_paws="4")
test_animal_form = AnimalForm(instance=test_animal)
assertEqual(test_animal_form.is_valid(), False) # No data has been supplied yet.
test_animal_form = AnimalForm({'name': "cat", 'number_paws': 4, }, instance=test_animal)
assertEqual(test_animal_form.is_valid(), True) # Now that you have given it data, it can validate.

有像
assertTrue
assertFalse