Django testing: Test the initial value of a form field

Belmin Fernandez picture Belmin Fernandez · Nov 21, 2010 · Viewed 7.2k times · Source

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.

Answer

Belmin Fernandez picture Belmin Fernandez · Nov 22, 2010

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.