测试Sanic文件上传

时间:2017-12-21 10:15:38

标签: aiohttp sanic

在查看docs时,我看到了一个很好的例子,说明如何测试一个健全的应用程序。

# 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)

它给出了一个错误,表明我无法传递文件。测试框架不支持这个吗?

1 个答案:

答案 0 :(得分:1)

事实证明,这类事情的标准是发布data参数。这很好用:

upload_payload = {'image':  open(os.path.join(IMG_DIR, img_name), 'rb')}
request, response = app.test_client.post('/image', data = upload_payload)