[Django]-Django testing: Test the initial value of a form field

38👍

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.

9👍

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)

2👍

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]
👤nate c

1👍

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)

0👍

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')
👤HBat

Leave a comment