sanic的pytest插件

pytest-sanic的Python项目详细描述


pytest sanic
==


开始徽章

…列表表::
:存根列:1

*-构建
-travis覆盖范围
*-文档
-文档
*-包
-版本轮子支持的版本支持的实现

|特拉维斯图片:https://travis-ci.org/yunstford/pytest-sanic.svg?branch=master
:alt:travis ci构建状态
:目标:https://travis ci.org/yunstford/pytest sanic

…|覆盖范围图片::https://coveralls.io/repos/github/yunstford/pytest-sanic/badge.svg?branch=master
:alt:coverage status
:target:https://coveralls.io/github/yunstford/pytest-sanic?分支=主

…|文档图片:https://readthedocs.org/projects/pytest-sanic/badge/?style=flat
:目标:https://readthedocs.org/projects/pytest sanic
:alt:文档状态

…|版本image::https://img.shields.io/pypi/v/pytest sanic.svg
:alt:pypi包最新版本
:目标:https://pypi.python.org/pypi/pytest sanic

| wheel image::https://img.shields.io/pypi/wheel/pytest sanic.svg
:alt:pypi wheel
:target:https://pypi.python.org/pypi/pytest sanic

……|支持的版本image::https://img.shields.io/pypi/pyversions/pytest sanic.svg
:alt:支持的版本
:目标:https://pypi.python.org/pypi/pytest sanic

…|支持的实现image::https://img.shields.io/pypi/implementation/pytest sanic.svg
:alt:支持的实现
:target:https://pypi.python.org/pypi/pytest sanic

end badges

sanic的pytest插件<;http://sanic.readthedocs.io/en/latest/>;`。它可以帮助您异步测试代码。

此插件提供:

*使用异步协同程序进行非常简单的测试
*常用和有用的设备
*异步设备支持
*用于SANIC应用程序的测试客户机/SANIC客户机
*用于SANIC的测试服务器应用程序


代码:

pip install pytest sanic


----
快速启动
----


您不必显式加载"pytest sanic"。`` pytest``会帮你的。只需编写像,

…代码块::python

async def test_sanic_db_find_by_id(app):
"
"假设,在db中,我们有
{
"id":"123",
"name":"Kobe Bryant",
"team":"湖人",
}
"
doc=await app.db["players"].find_by_id("123")
assert doc.name=="Kobe Bryant"
assert doc.team=="Laxes"



这里有一个更现实、更有用的例子,

……代码块::python


from.app import create嫒app

@pytest.yield嫒fixture
def app():
app=create嫒app(test嫒config,**params)
yield app

@pytest.fixture
def test嫒cli(loop,app,sanic_client):
返回循环。运行直到完成(sanic_client(app))


async def test_index(test_cli):
resp=await test_cli.get('/')
assert resp.status==200

async def test_player(test_cli):
resp=awaittest_cli.get('/player')
assert resp.status==200



---------
异步fixture
---------

``pytest sanic``也支持异步fixture,只需像普通pytest fixture一样写入它们即可。

。代码块::python

@pytest.fixture
async def async_fixture_sleep():
等待asyncio.sleep(0.1)
返回"sleep…"


--
fixtures
--

ting.

``loop`
~~~~~~~~~~

``pytest sanic``创建一个事件循环并将其作为fixture注入。`` pytest``将使用此事件循环来运行``async tests`。
默认情况下,fixture``loop`是` asyncio.new_event_loop`的一个实例。但是"uvloop"也是您的一个选项,通过简单的传递
`--loop uvloop``。请记住只使用一个事件循环。


``未使用的端口`
~~~~~~~~~~~~~~~~~~

>本地主机上未使用的TCP端口。



``测试服务器`
~~~~~~~~~~~~~

通过提供一个"sanic"应用程序创建一个TestServer实例。利用"测试服务器"很容易创建用于测试的"sanic"
应用程序服务器。代码块::python

@pytest.yield嫒fixture
def app():
app=sanic("test嫒sanic嫒app")

@app.route("/test嫒get",方法=['get'])
async def test-get(请求):
return response.json({"get":true})


yield app

@pytest.fixture
def sanic-u server(loop,app,test-server):
return loop.run直到完成(test-server(app))

很容易通过创建自己的"loop"fixture来覆盖这个"loop"fixture,就像,

…代码块::python

@pytest.yield\fixture
def loop():
loop=myeventloop()
yield loop
loop.close()

`` test\u client```
~~~~~~~~~~~~~~

`` test\u client``已被弃用,请改用'sanic\u client',查看"issue<;https://github.com/yunstford/pytest-sanic/issues/22>;``了解更多上下文。



``sanic-u-client`
~~~~~~~~~~~~~~

通过提供"sanic"应用程序创建一个testclient实例。使用"sanic客户机"就可以简单地拥有一个客户机,比如

…代码块::python

@pytest.yield嫒fixture
def app():
app=sanic("test嫒sanic嫒app")

@app.route("/test嫒get",方法=['get'])
异步定义测试获取(请求):
返回response.json({"get":true})

@app.route("/test\u post",方法=['post'])
异步def test_post(请求):
返回response.json({"post":true})

@app.route("/test_put",methods=['put'])
异步def test-put(请求):
返回response.json({"put":true})

@app.route("/test-delete",methods=['delete'])
异步定义test_delete(请求):
返回response.json({"delete":true})

@app.route("/test_patch",methods=['patch'])
异步定义测试修补程序(请求):
返回response.json({"patch":true})

@app.route("/test\u options",methods=['options'])
异步定义测试选项(请求):
返回response.json({"options":true})

@app.route("/test_head",methods=['head'])
异步定义测试头(请求):
返回response.json({"head":true})

@app.websocket("/test_ws")
异步定义测试头(请求,ws):
data=等待ws.recv()
等待ws.send(data)


@pytest.fixture
def test_cli(loop,app,sanic_client):
返回loop.run_直到完成(sanic_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}

==200
resp_json=await resp.json()
assert resp json={"post":true}


async def test_fixture_test_client_put(test_cli):
"
put request
""
resp=awit test_cli.put('/test_put')
assert resp.status==200
resp撸json=wait resp.json()
assert resp json={"put":true}


async def test_test_client_delete(test_cli):
"
delete request
""
resp=wait test_cli.delete('/test_delete')
assert resp.status==200
resp_json=await resp.json()
断言resp_json={"删除":true}


async def test_fixture_test_client_patch(test_cli):
"
补丁请求
""
resp=await test_cli.patch('/test_patch')
断言resp.status==200
resp_json=await resp.json()
assert resp_json={"patch":true}


async def test_fixture_test_client_options(test_cli):
"
options request
""
resp=await test_cli.options('/test_options')
assertresp.status==200
resp琰son=await resp.json()
assert resp琰son=={"options":true}


async def test琰u test琰u client琰head(test琰cli):
"
head request
"
resp=await琰u cli head('/test琰head')
assert resp.status==200
resp json=await resp.json()
test_cli.ws_connect('/test_ws')
数据="你好,世界!"
await ws_conn.send_str(data)
msg=await ws_conn.receive()
assert msg.data==data
await ws_conn.close()



小注释:

`` test_cli.ws_connect``在`` sanic中不起作用。''suu version_uu<;='0.5.4'``',因为sanic错误,但它已被固定在主分支中。和"websockets.\uu version='4.0'``在"sanic.\uu version='0.6.0'``中已断开websockets,但已在"master<;https://github.com/channelcat/sanic/commit/bca1e084116335fd939c2ee226070f0428cd5de8>中修复;`.


----
tips
---

*`蓝图测试<;https://github.com/yunstford/pytest sanic/issues/3>;` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `应用程序已经在运行循环<;https://github.com/yunstford/pytest sanic/issues/1>;``当您有'db洋init'侦听器时。
*`不正确的覆盖率报告<;https://github.com/pytest dev/pytest cov/issues/117>;``使用'pytest cov`,但是我们可以为这个问题提供解决方法,这本质上是一个pytest加载插件的问题。
*websockets>;4.0已经在"sanic"中破坏了websockets,但是已经在"master"中修复了,https://github.com/channelcat/sanic/commit/bca1e084116335fd939c2ee226070f0428cd5de8>;` ` `


如果你有任何问题的话。您还可以查看"已解决的问题"http://github.com/yunstford/pytest-sanic/issues?q=is%3aclosed>;``u


——
——
——
——
——

``pytest sanic``接受对github的贡献,以问题或请求的形式。



运行单元测试。

代码:

./铀测试


-
-
参考
-
-


一些有用的Pytest插件:

*`Pytest Tornado<;https://github.com/eugeny/Pytest Tornado>;`\br/>*`Pytest Asyncio<;https://github.com/Pytest dev/Pytest Asyncio>;`
*`pytest aiohttp<;https://github.com/aio-libs/pytest aiohttp>;`

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Maven中的java,为什么要运行“mvn clean”?   java会降低图像质量。OutOfMemory异常Android   在Java8中将函数传递到流的过滤方法   jboss6。x java。lang.NoClassDefFoundError,当我将<listenerclass>包含到web时。xml java   java读取图像像素时,像素Alpha始终为255   java在迭代后跳过一行   java如何创建我自己的单链表   意图上的java空指针异常。getStringExtra.:安卓   具有连接实体的java Hibernate onetoone映射   java需要帮助在自制的仓鼠模拟器上实现启动/恢复/暂停/停止线程操作   如何测试非主方法?[Java,IntelliJ]   java jdbc自动提交(false)不起作用   java在JADE中的同一容器中创建多个代理   java OkHttp获取失败的响应正文   java Webdriver flash按钮