Django testing: Test the initial value of a form f

2020-05-31 06:06发布

I have a view that should be setting an initial value for a form field based on a GET value. I want to test this. I'm currently using Django's test client but I am open to looking at other tools.

Edit

Sorry, I did not mention that I am well aware of the assertContains method but I was hoping there was a better way other than searching the HTML for an input tag and the value attribute.

5条回答
▲ chillily
2楼-- · 2020-05-31 06:19

I think this feature comes with 1.3 but it may have come in earlier. I've slightly modified the example on the page to work with your requirements, but it's untested code and I've assumed a few things like the form parameter of the response context. Modify as applicable. The point of this answer is to show the request factory.

http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.RequestFactory

from django.utils import unittest
from django.test.client import RequestFactory

class SimpleTest(unittest.TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()

    def test_details(self):
        get_param = 'some_value'
        # Create an instance of a GET request.
        request = self.factory.get('/customer/details/?param={0}'.format(get_param))

        # Test my_view() as if it were deployed at /customer/details
        response = my_view(request)

        # test 1
        form = response.form
        idx = form.as_p().find(get_param)
        self.assertNotEqual(idx, -1)            
        #or.. test 2
        self.assertContains(response, get_param)
查看更多
Bombasti
3楼-- · 2020-05-31 06:27

The value will be embedded in the html as <input value= 'whatever'/>. You can search for that string with whatever tool you prefer.

response = Client().get('/customer/details/')
print [line for line in response.split('\n') if line.find('<input') > -1]
查看更多
看我几分像从前
4楼-- · 2020-05-31 06:27

If you strictly checking for the initial value of a form field, another alternative is testing your form:

forms.py:

from django import forms

class MyForm(forms.Form):
    title = forms.CharField(initial='My Default Title')

test_forms.py

from django.test import TestCase
from .forms import MyForm

class MyFormTests(TestCase):
    def test_myform_initial_value(self):
        form = MyForm()
        self.assertEqual(form['title'].initial, 'My Default Title')
查看更多
放荡不羁爱自由
5楼-- · 2020-05-31 06:34

Hate to answer my own question (like the 3rd time I've done it) but after mocking around with the test client, I've found a better way:

def test_creating_stop(self):
    c = self.client

    # Check that name is pre-filled
    response = c.get('%s?name=abcd' % reverse('add_new_stop'))
    self.assertEqual(response.context['form'].initial['name'], 'abcd')

Does anyone see anything wrong with this? I'll leave it up for a while see what people think.

查看更多
不美不萌又怎样
6楼-- · 2020-05-31 06:41

The accepted solution check initial['...'] value on the form but you could also check the actual value on the field. Pseudo-code bellow.

This is helpful if you want to test a default value coming directly from the model (form.initial is not set) and to make sure that initial['...'] is the actual value.

def test_some_default_value(self):
        response = self.client.get('/signup/')
        self.assertEquals(response.context['form']['plan'].value(), my_value)

def test_some_default_value_2(self):
        some_different_conditions...
        response = self.client.get('/signup/')
        self.assertEquals(response.context['form']['plan'].value(), a_different_value)
查看更多
登录 后发表回答