Python Pyramid 单元测试用例 Dummyrequest:需要将文件路径转换为“webob.compat.cgi_FieldStorage”
我在金字塔中创建了以下 API。
@view_config(route_name="/demo", renderer="json", request_method="POST")
def file_attachment(self):
file = self.request.POST['file']
file_content = file.file
file_content.seek(0)
API 将 username
和 form-data: file
作为输入。
我正在使用pyramid.testing.Dummyrequest为此API创建单元测试用例,如下所示:
def test_img_attach_success(self):
input_data = {
"username": "[email protected]",
"file": "/tmp/test_inputs/demo_img.png"
}
self.collab.request = testing.DummyRequest(POST=input_data, dbsession=self.session)
response = self.collab.file_attachment()
在测试用例中,我将file
的值作为字符串传递。但 file_attachment
函数中的 file
变量类型采用 webob.compat.cgi_FieldStorage
。
所以我收到 AttributeError: 'str' object has no attribute 'file'。
如何将 str
转换为 webob.compat.cgi_FieldStorage
?
I have created following API in pyramid.
@view_config(route_name="/demo", renderer="json", request_method="POST")
def file_attachment(self):
file = self.request.POST['file']
file_content = file.file
file_content.seek(0)
API takes username
and form-data: file
as an input.
I am creating unit testcase for this API using pyramid.testing.Dummyrequest
as follows:
def test_img_attach_success(self):
input_data = {
"username": "[email protected]",
"file": "/tmp/test_inputs/demo_img.png"
}
self.collab.request = testing.DummyRequest(POST=input_data, dbsession=self.session)
response = self.collab.file_attachment()
in testcase I am passing value of file
as a string. but type of file
variable in file_attachment
function taking webob.compat.cgi_FieldStorage
.
So I am getting AttributeError: 'str' object has no attribute 'file'
.
How can I convert str
to webob.compat.cgi_FieldStorage
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论