我应该如何在Django中编写有关Forms的测试?

问题:我应该如何在Django中编写有关Forms的测试?

在编写测试时,我想在Django中模拟对视图的请求。这主要是测试表格。这是一个简单的测试请求的片段:

from django.tests import TestCase

class MyTests(TestCase):
    def test_forms(self):
        response = self.client.post("/my/form/", {'something':'something'})
        self.assertEqual(response.status_code, 200) # we get our page back with an error

无论是否存在表单错误,页面始终返回200的响应。如何检查我的表格是否失败以及特定字段(soemthing)是否存在错误?

I’d like to simulate requests to my views in Django when I’m writing tests. This is mainly to test the forms. Here’s a snippet of a simple test request:

from django.tests import TestCase

class MyTests(TestCase):
    def test_forms(self):
        response = self.client.post("/my/form/", {'something':'something'})
        self.assertEqual(response.status_code, 200) # we get our page back with an error

The page always returns a response of 200 whether there’s an form error or not. How can I check that my Form failed and that the particular field (soemthing) had an error?


回答 0

我认为,如果您只想测试表单,则应该只测试表单,而不是测试呈现表单的视图。举例说明:

from django.test import TestCase
from myapp.forms import MyForm

class MyTests(TestCase):
    def test_forms(self):
        form_data = {'something': 'something'}
        form = MyForm(data=form_data)
        self.assertTrue(form.is_valid())
        ... # other tests relating forms, for example checking the form data

I think if you just want to test the form, then you should just test the form and not the view where the form is rendered. Example to get an idea:

from django.test import TestCase
from myapp.forms import MyForm

class MyTests(TestCase):
    def test_forms(self):
        form_data = {'something': 'something'}
        form = MyForm(data=form_data)
        self.assertTrue(form.is_valid())
        ... # other tests relating forms, for example checking the form data

回答 1

https://docs.djangoproject.com/zh-CN/stable/topics/testing/tools/#django.test.SimpleTestCase.assertFormError

from django.tests import TestCase

class MyTests(TestCase):
    def test_forms(self):
        response = self.client.post("/my/form/", {'something':'something'})
        self.assertFormError(response, 'form', 'something', 'This field is required.')

其中“ form”是表单的上下文变量名称,“ something”是字段名称,而“此字段是必需的”。是预期验证错误的确切文本。

https://docs.djangoproject.com/en/stable/topics/testing/tools/#django.test.SimpleTestCase.assertFormError

from django.tests import TestCase

class MyTests(TestCase):
    def test_forms(self):
        response = self.client.post("/my/form/", {'something':'something'})
        self.assertFormError(response, 'form', 'something', 'This field is required.')

Where “form” is the context variable name for your form, “something” is the field name, and “This field is required.” is the exact text of the expected validation error.


回答 2

2011年的原始答案是

self.assertContains(response, "Invalid message here", 1, 200)

但是我现在看到(2018)有大量适用的断言可用

  • assertRaisesMessage
  • assertFieldOutput
  • assertFormError
  • assertFormsetError

随便你吧。

The original 2011 answer was

self.assertContains(response, "Invalid message here", 1, 200)

But I see now (2018) there is a whole crowd of applicable asserts available:

  • assertRaisesMessage
  • assertFieldOutput
  • assertFormError
  • assertFormsetError

Take your pick.