Friday, February 03, 2012

Python/Django unittesting and formsets

In recent months I have been getting to grips with Python/Django. It all started back at the Health 2.0 Developer Challenge when I worked with Alan Viars of Videntity to build the We built the application using Python/Django and ran it on Amazon's cloud.

Now I am working on writing a series of Unit Tests for an application. It has been going pretty well but now I am hitting a road block and wonder if anyone else has had similar experience (and hopefully solutions).

I am tring to test a web page that uses multiple sections to the page and also uses formsets.

I get the error:
u'ManagementForm data is missing or has been tampered with'
when I try to post to the page using the UnitTest framework.

I have read various solutions on but I am still hitting that wall.

I have looked through the source of the page and extracted field names (there are hundreds in the whole form - you know what government forms are like!) 

The test I am trying to perform is to submit an incomplete form and test for an error message. However, the unittest bombs with the ManagementForm Data error.

Here is the data I am submitting (HIPAA disclaimer - all data is fictitious):

post_parameters = {'medication_assistance':"on",
                           'coinhabitants-0-TOTAL_FORMS': u'1',
                           'coinhabitants-0-INITIAL_FORMS': u'0',


Every Django formset comes with a management form that needs to be included in the post. The official docs explain it pretty well. To use it within your unit test, you either need to write it out yourself. (The link I provided shows an example), or call formset.management_form which outputs the data.
[Provided by Bartek]

What is not clear to me is how to include the management_form data. 

Can anyone shed any light on what might need to be in the post parameters?

Posted via email from ekivemark: pre-blogspot