[英]Getting 400 on Flask Unit Test
我正在為Flask中的文件上傳功能編寫單元測試。
該應用程序在本地運行良好,但單元測試拋出400。謝謝!
這是我的app.py
from flask import Flask, render_template, request
app = Flask(__name__)
app.config["TESTING"] = True
@app.route('/')
def index():
return render_template('index.html')
@app.route('/uploader', methods = ['GET','POST'] )
def upload_photo():
if request.method == 'POST':
photo = request.files['photo']
resp = app.make_response(photo.read())
resp.mimetype = 'image/jpeg'
return resp
if __name__ == '__main__':
app.run(debug=True)
這是我的測試:
import unittest
import io
from app import app
class TestCase(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def test_upload_photo(self):
result = self.client.post('/uploader',
content_type='multipart/form-data', follow_redirects=True,
data=dict(upload_var=(io.BytesIO(b'photo'), 'photo')))
print(result.data)
self.assertTrue(result.status_code == 200)
if __name__ == "__main__":
unittest.main()`
在數據字典upload_var
更改為photo
,因為這是您的處理程序函數期望調用該文件的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.