6

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

Mr-F
  • 871
  • 7
  • 12
  • Did you try using form.set('thumbnail', '', index=0)? – pi. Oct 17 '12 at 20:30
  • Thanks @pi for your comment, however, from my understanding the index value is only needed if their are multiple fields with the same name on the form. I tried it just to be on the safe side, but it didn't do anything different. There is obviously something else that I'm missing here. – Mr-F Oct 24 '12 at 13:42

1 Answers1

2

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.

Philippe Ombredanne
  • 2,017
  • 21
  • 36
  • Yes, actually the original version didn't have the logging in. So removing this won't have any affect on the results I'm getting at the moment. Thanks for taking the time though. – Mr-F Jan 09 '13 at 09:48