I've been searching the web for the last two days trying to understand the problem I'm having with WebTest. However, I have had no joy, and was wondering if anyone here might be able to help.
I'm using nose to run test on a web application that I'm developing but seem to be having problems with a form that has a file upload field in it. The form and validation works on the server when it is running normally, and if I run the test code from a shell it works as well. However, whenever I run the test code from nose it fails to accept the submitted information.
Here is an example of the form:
<form method="POST" enctype="multipart/form-data" action="....">
<input type="text" name="first_name" id="first_name">
<input type="text" name="last_name" id="last_name">
<input type="file" name="thumbnail" id="thumbnail">
<input type="submit" value="Create" name="submit" id="submit">
</form>
My WebTest code looks like this:
response = self.app.get( url(controller=self.controller, action='create') )
form = response.form
log.debug( form.submit_fields() )
form.set('first_name', 'test1-1')
form.set('last_name', 'test1-1')
form.set('thumbnail', '')
log.debug( form.submit_fields() )
response = form.submit()
The response I get when I run this is that thumbnail is missing from the submitted values, even thought the field isn't required by the form validator. When I compared the code output from Nose and when running it through a shell I noticed that the output from submit_fields was different
Shell Output:
[('first_name', ''),('last_name', '')] #First log call
[('first_name', 'test1-1'),('last_name', 'test1-1'), ('thumbnail', '')] #Second log call
Nose Output:
[(u'first_name', ''), (u'last_name', ''), (u'thumbnail', <File name="thumbnail" id="thumbnail">)] #First log call
[(u'first_name', 'test1-1'), (u'last_name', 'test1-1'),(u'thumbnail', <File name="thumbnail" id="thumbnail">)] #Second log call
As you can see there is a difference in the shell doesn't have the thumbnail tuple, but sets it to a empty string which passes through without a problem. However, in Nose there is already a tuple there and it doesn't reset the value. Can anyone help me with this? Is there a problem with trying multipart forms in WebTest when using the form.submit approach?
Thanks in advance for your help.
Library Information: Pylons-1.0.1 WebTest-1.4.0 WebOb-1.2.3 nose-1.2.1
Have you tried removing the log.debug in log.debug( form.submit_fields() )
?
Nose has sometimes been known to interact weirdly with logging as it does some internal redirection of outputs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With