python - nosetests 和文件上传问题

标签 python file-upload pylons nose webtest

过去两天我一直在网上搜索,试图了解我在使用 WebTest 时遇到的问题。但是,我并不高兴,想知道这里是否有人可以提供帮助。

我正在使用 nose 在我正在开发的 Web 应用程序上运行测试,但似乎在处理其中包含文件上传字段的表单时遇到问题。当服务器正常运行时,表单和验证在服务器上工作,如果我从 shell 运行测试代码,它也能正常工作。但是,每当我从 nose 运行测试代码时,它都无法接受提交的信息。

下面是一个例子:

<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>

我的 WebTest 代码如下所示:

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()

我运行此命令时得到的响应是提交的值中缺少缩略图,即使该字段不是表单验证程序所必需的。当我比较 Nose 的代码输出和通过 shell 运行它时,我注意到 submit_fields 的输出不同

外壳输出:

[('first_name', ''),('last_name', '')] #First log call
[('first_name', 'test1-1'),('last_name', 'test1-1'), ('thumbnail', '')] #Second log call

Nose 输出:

[(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

如您所见,shell 没有缩略图元组,但将其设置为空字符串,可以顺利通过。但是,在 Nose 中已经有一个元组,它不会重置值。谁能帮我这个?使用 form.submit 方法时在 WebTest 中尝试多部分表单是否有问题?

预先感谢您的帮助。

图书馆信息: Pylons-1.0.1 WebTest-1.4.0 WebOb-1.2.3 nose-1.2.1

最佳答案

您是否尝试过删除 log.debug( form.submit_fields() ) 中的 log.debug?

众所周知,Nose 有时会与日志记录进行奇怪的交互,因为它会对输出进行一些内部重定向。

关于python - nosetests 和文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892005/

相关文章:

python - Flask CSRF 保护在响应 header 上设置无效的 Set-Cookie

python - Pandas 将两列值添加到新数据框

web-applications - 是否有用于从 Web 应用程序上传 Assets 的 API/服务?

python - 从 Pylons 发送电子邮件

python - 如何在脚本中使用 Python REPL

python - 为什么在 Python 中 open() 优于 file()?

javascript - jQuery 文件上传 - 单次上传多次发送数据

javascript - Meteor:无法使用 CollectionFS 将图像上传到 S3

python - 在 Pyramid 应用程序中记录到 SQLAlchemy 记录器

python - Pylons Web 应用程序的最小 Linux?