我正在努力为负责上传从页面上的表单收到的图片的功能创建单元测试。
主要问题是我无法弄清楚如何将图片添加到虚拟请求的发布参数中,并因此将其传递给函数。
这是我正在尝试测试的代码。
谢谢
@view_config(route_name='profile_pic')
def profilePictureUpload(request):
if 'form.submitted' in request.params:
#max picture size is 700kb
form = Form(request, schema=PictureUpload)
if request.method == 'POST' and form.validate():
upload_directory = 'filesystem_path'
upload = request.POST.get('profile')
saved_file = str(upload_directory) + str(upload.filename)
perm_file = open(saved_file, 'wb')
shutil.copyfileobj(upload.file, perm_file)
upload.file.close()
perm_file.close()
else:
log.info(form.errors)
redirect_url = route_url('profile', request)
return HTTPFound(location=redirect_url)