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.
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
The value will be embedded in the html as <input value= 'whatever'/>. You can search for that string with whatever tool you prefer.
If you strictly checking for the initial value of a form field, another alternative is testing your form:
forms.py:
test_forms.py
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:
Does anyone see anything wrong with this? I'll leave it up for a while see what people think.
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.