测试 Sanic 文件上传
Testing Sanic file Uploads
在查看 docs 时,我看到了一个很好的示例,说明如何测试 sanic 应用程序。
# Import the Sanic app, usually created with Sanic(__name__)
from external_server import app
def test_index_returns_200():
request, response = app.test_client.get('/')
assert response.status == 200
def test_index_put_not_allowed():
request, response = app.test_client.put('/')
assert response.status == 405
现在我正在尝试让测试框架接受上传到端点的照片。我的代码通过以下方式工作:
upload_payload = {'image': open(os.path.join(img_dir, img_name), 'rb')}
request, response = app.test_client.post('/image', file = upload_payload)
它给出了一个错误,提示我无法传递文件。测试框架不支持这个吗?
原来这些事情的标准是发布一个 data
参数。这很好用:
upload_payload = {'image': open(os.path.join(IMG_DIR, img_name), 'rb')}
request, response = app.test_client.post('/image', data = upload_payload)
在查看 docs 时,我看到了一个很好的示例,说明如何测试 sanic 应用程序。
# Import the Sanic app, usually created with Sanic(__name__)
from external_server import app
def test_index_returns_200():
request, response = app.test_client.get('/')
assert response.status == 200
def test_index_put_not_allowed():
request, response = app.test_client.put('/')
assert response.status == 405
现在我正在尝试让测试框架接受上传到端点的照片。我的代码通过以下方式工作:
upload_payload = {'image': open(os.path.join(img_dir, img_name), 'rb')}
request, response = app.test_client.post('/image', file = upload_payload)
它给出了一个错误,提示我无法传递文件。测试框架不支持这个吗?
原来这些事情的标准是发布一个 data
参数。这很好用:
upload_payload = {'image': open(os.path.join(IMG_DIR, img_name), 'rb')}
request, response = app.test_client.post('/image', data = upload_payload)