如何从测试函数定义 request.files['file']?
How to Define request.files['file'] from test function?
我需要测试将新用户注册到我的数据库并在文件系统中为该用户创建个人资料图像的功能。
通常情况下,该函数会在提交表单并将名称、phone、密码...和图像传递给函数时运行。
因此,此函数需要 request.files['file'] 保存图像。
如何调用函数并预定义 request.files['file'] 的值,因为它持有 .jpg 图片?
这是我的测试用例,没有考虑所需的图像!
def registering(self, username, email, password, passwordCheck, phone):
return self.app.post('/registering', data=dict(
username=username,
email=email,
password=password,
passwordCheck=passwordCheck,
phone=phone
), follow_redirects=True)
def test_registering(self):
rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
assert 'Registered Successfully' in rv.data
这是一个解决方案,花了我一些时间但是...:
def registering(self, username, email, password, passwordCheck, phone):
with open('static/test.jpg') as test:
imgStringIO = StringIO(test.read())
return self.app.post('/registering',
content_type='multipart/form-data',
data=dict(
{'file': (imgStringIO, 'test.jpg')},
username=username,
email=email,
password=password,
passwordCheck=passwordCheck,
phone=phone
), follow_redirects=True
)
def test_03_registering(self):
rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
assert 'Registered Successfully' in rv.data
我需要测试将新用户注册到我的数据库并在文件系统中为该用户创建个人资料图像的功能。
通常情况下,该函数会在提交表单并将名称、phone、密码...和图像传递给函数时运行。
因此,此函数需要 request.files['file'] 保存图像。
如何调用函数并预定义 request.files['file'] 的值,因为它持有 .jpg 图片?
这是我的测试用例,没有考虑所需的图像!
def registering(self, username, email, password, passwordCheck, phone):
return self.app.post('/registering', data=dict(
username=username,
email=email,
password=password,
passwordCheck=passwordCheck,
phone=phone
), follow_redirects=True)
def test_registering(self):
rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
assert 'Registered Successfully' in rv.data
这是一个解决方案,花了我一些时间但是...:
def registering(self, username, email, password, passwordCheck, phone):
with open('static/test.jpg') as test:
imgStringIO = StringIO(test.read())
return self.app.post('/registering',
content_type='multipart/form-data',
data=dict(
{'file': (imgStringIO, 'test.jpg')},
username=username,
email=email,
password=password,
passwordCheck=passwordCheck,
phone=phone
), follow_redirects=True
)
def test_03_registering(self):
rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
assert 'Registered Successfully' in rv.data