Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing a Django Form Containing Multiple Submit Buttons

I am writing unit tests for a page that uses several Submit buttons to control logical flow through my Django application.

Unfortunately, I can't figure out how to get the response to return the submit values in the unit testing framework. The Django unit testing documentation for post indicates its form is the following:

post(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)

In the case of a Delete button of the form:

<input type="submit" name="delete" value="Delete" />

I've tried placing the Delete value in as data, i.e.:

response = self.client.post(url, {'name':'delete'}, follow=True)

but that doesn't seem to work. I need to have the name values in order to exercise the code paths that they trigger. In the views, the logic takes the form of:

if 'delete' in request.POST:
    <do something>

I'm assuming that I make use of **extra somehow to get these values but I haven't had much luck with it either.

Any suggestions?

like image 621
Sinidex Avatar asked Nov 18 '10 17:11

Sinidex


1 Answers

The data dictionary should map input names to values. In your case, the name is delete, and the value is Delete. So the dictionary should be:

{'delete': 'Delete'}
like image 152
Daniel Roseman Avatar answered Oct 29 '22 06:10

Daniel Roseman