我需要测试一个将新用户注册到我的数据库的函数,并在文件系统中为该用户创建一个配置文件图像。
通常,当提交表单时,函数会运行,将名称,电话,密码和图像传递给函数。
因此,此函数需要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
答案 0 :(得分:0)
这是一个解决方案,花了我一些时间,但......:
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