Sanic 路由节点测试可以通过test_client
对象进行,它依赖于aiohttp
库。
test_client测试
这个test_client
对象提供了get
, post
, put
, delete
, patch
, head
和options
方法来测试我们的web 应用。
下面是一个简单的实例:
# 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
在内部,我们每次调用test_client
的方法,Sanic app 就会运行在 127.0.0.1:42101
,测试请求使用aiohttp
来访问应用程序。
test_client
的方法接受以下参数及关键字参数:
uri
(默认为/
)表示要测试的URI的字符串;gather_request
(默认为True
)布尔型,决定是否要返回原始请求。如果设置为True
,返回值为一个tuple:(request, response)
,如果为False
则只返回response。server_kwargs
(默认为{}
),一个字典用以存放传递给app.run
的额外参数的字典;debug
(默认为False
)布尔型,决定是否要以调试模式运行server。
还有两个参数:*request_args
和**request_kwargs
,它们将被直接传递给aiohttp ClientSession。
例如,下面这种给 Get 请求传递数据:
def test_get_request_includes_data():
params = {'key1': 'value1', 'key2': 'value2'}
request, response = app.test_client.get('/', params=params)
assert request.args.get('key1') == 'value1'
给 POST 请求传递 JSON 数据:
def test_post_json_request_includes_data():
data = {'key1': 'value1', 'key2': 'value2'}
request, response = app.test_client.post('/', data=json.dumps(data))
assert request.json.get('key1') == 'value1'
使用随机端口
如果我们想使用空闲的非Linux内核选定端口取代SanicTestClient
默认端口,我们可以通过指定posrt=None
来实现。在大多数系统中,随机端口的范围在 1024 到 65535 之间。
# Import the Sanic app, usually created with Sanic(__name__)
from external_server import app
from sanic.testing import SanicTestClient
def test_index_returns_200():
request, response = SanicTestClient(app, port=None).get('/')
assert response.status == 200
pytest-sanic 插件
pytest-sanic
是一个 pytest 插件,它帮助我们以异步方式测试我们的代码。通常以下面的方式写test case:
async def test_sanic_db_find_by_id(app):
"""
Let's assume that, in db we have,
{
"id": "123",
"name": "Kobe Bryant",
"team": "Lakers",
}
"""
doc = await app.db["players"].find_by_id("123")
assert doc.name == "Kobe Bryant"
assert doc.team == "Lakers"
pytest-sanic
还提供些非常有用的小部件,比如loop,unused_port, test_server, test_client。
@pytest.yield_fixture
def app():
app = Sanic("test_sanic_app")
@app.route("/test_get", methods=['GET'])
async def test_get(request):
return response.json({"GET": True})
@app.route("/test_post", methods=['POST'])
async def test_post(request):
return response.json({"POST": True})
yield app
@pytest.fixture
def test_cli(loop, app, test_client):
return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))
#########
# Tests #
#########
async def test_fixture_test_client_get(test_cli):
"""
GET request
"""
resp = await test_cli.get('/test_get')
assert resp.status == 200
resp_json = await resp.json()
assert resp_json == {"GET": True}
async def test_fixture_test_client_post(test_cli):
"""
POST request
"""
resp = await test_cli.post('/test_post')
assert resp.status == 200
resp_json = await resp.json()
assert resp_json == {"POST": True}

我的公众号:猿人学 Python 上会分享更多心得体会,敬请关注。
***版权申明:若没有特殊说明,文章皆是猿人学 yuanrenxue.con 原创,没有猿人学授权,请勿以任何形式转载。***
说点什么吧...