测试

Sanic 端可以使用 test_client 对象进行本地测试,该对象基于额外的 aiohttp 库。

test_client 公开 get, post, put, delete, patch, headoptions 方法供你针对你的应用程序运行。一个简单的例子 (using pytest) 如下:

  1. # Import the Sanic app, usually created with Sanic(__name__)
  2. from external_server import app
  3. def test_index_returns_200():
  4. request, response = app.test_client.get('/')
  5. assert response.status == 200
  6. def test_index_put_not_allowed():
  7. request, response = app.test_client.put('/')
  8. assert response.status == 405

在内部,每次你调用 test_client 方法的任一,Sanic 会在 127.0.0.1:42101 执行并且你的测试请求会使用 aiohttp 针对你的应用程序进行执行。

test_client 方法接收如下参数和关键字参数:

  • uri (默认 '/') 一个要测试的 URI 字符串。
  • gather_request (默认 True) 一个确定是否被函数返回原始请求的布尔值。如果设置为 True,返回值十一个 (request, response) 的元组,如果是 False 就只返回响应。
  • server_kwargs (默认 {}) 一个在测试请求执行之前传入 app.run 的额外参数的字典。
  • debug (默认 False) 一个确定是否在调试模式启动服务的布尔值。

程序进一步采用 *request_args**request_kwargs,它们直接传递给 aiohttp ClientSession 请求。

例如,要提供数据给一个 GET 请求,你可以操作如下:

  1. def test_get_request_includes_data():
  2. params = {'key1': 'value1', 'key2': 'value2'}
  3. request, response = app.test_client.get('/', params=params)
  4. assert request.args.get('key1') == 'value1'

要提供数据给一个 JSON POST 请求:

  1. def test_post_json_request_includes_data():
  2. data = {'key1': 'value1', 'key2': 'value2'}
  3. request, response = app.test_client.post('/', data=json.dumps(data))
  4. assert request.json.get('key1') == 'value1'

更多关于 aiohttp 有效参数的信息可以在 in the documentation for ClientSession 找到。

pytest-sanic

pytest-sanic 十一个 pytest 插件,它帮助你异步地测试你的代码。写法如下:

  1. async def test_sanic_db_find_by_id(app):
  2. """
  3. Let's assume that, in db we have,
  4. {
  5. "id": "123",
  6. "name": "Kobe Bryant",
  7. "team": "Lakers",
  8. }
  9. """
  10. doc = await app.db["players"].find_by_id("123")
  11. assert doc.name == "Kobe Bryant"
  12. assert doc.team == "Lakers"

pytest-sanic 还提供了一些有用的装置,如 loop, unused_port, test_server, test_client。

  1. @pytest.yield_fixture
  2. def app():
  3. app = Sanic("test_sanic_app")
  4. @app.route("/test_get", methods=['GET'])
  5. async def test_get(request):
  6. return response.json({"GET": True})
  7. @app.route("/test_post", methods=['POST'])
  8. async def test_post(request):
  9. return response.json({"POST": True})
  10. yield app
  11. @pytest.fixture
  12. def test_cli(loop, app, test_client):
  13. return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))
  14. #########
  15. # Tests #
  16. #########
  17. async def test_fixture_test_client_get(test_cli):
  18. """
  19. GET request
  20. """
  21. resp = await test_cli.get('/test_get')
  22. assert resp.status == 200
  23. resp_json = await resp.json()
  24. assert resp_json == {"GET": True}
  25. async def test_fixture_test_client_post(test_cli):
  26. """
  27. POST request
  28. """
  29. resp = await test_cli.post('/test_post')
  30. assert resp.status == 200
  31. resp_json = await resp.json()
  32. assert resp_json == {"POST": True}