跳转到主要内容

未提供项目描述

项目描述

===============
testing-aiohttp
===============

.. 图像:: https://travis-ci.org/genericclient/testing-aiohttp.svg?branch=master
:target: https://travis-ci.org/genericclient/testing-aiohttp

`aiohttp`的测试工具。仅支持Python 3.5及以上。


安装
============

::

$ pip install testing-aiohttp

用法
=====

``RouteManager``
---------------------------

``RouteManager``将为`aiohttp.Client``模拟响应。

API受``responses``库的启发:

from aiohttp import ClientSession
from asynctest import TestCase

from testing_aiohttp import RouteManager


# 创建您的测试。
class MyTestCase(TestCase)
async def test_response_data(self)
with RouteManager() as rsps
rsps.add('GET', 'http://example.org/users', json=[
{
'id': 1,
'username': 'user1',
'group': 'watchers',
},
{
'id': 2,
'username': 'user2',
'group': 'watchers',
},
])

async with ClientSession() as session
response = await session.get('http://example.org/users')
self.assertEqual(response, 200)
users = await response.json()
self.assertEqual(len(users), 2)

::

from aiohttp import ClientSession
from asynctest import TestCase

from testing_aiohttp import RouteManager


async def request_callback(request)
return (200, {}, 'ok')


class MyTestCase(TestCase)

async def test_endpoint_detail_route(self)
with RouteManager() as rsps
rsps.add_callback(
'POST', 'http://example.org/users/2/notify',
callback=request_callback,
content_type='application/json',
)

async with ClientSession() as session
response = await session.post('http://example.org/users/2/notify')
self.assertEqual(await response.text(), 'ok')

::
from aiohttp import ClientSession
from asynctest import TestCase

from testing_aiohttp.rsps import RouteManager, RouteNotFoundError


class MyTestCase(TestCase)
async def test_response_match_querystring(self)
with RouteManager() as rsps
rsps.add('GET', 'http://example.org/users?username=user1', json=[
{
'id': 1,
'username': 'user1',
'group': 'watchers',
},
], match_querystring=True)

with ClientSession() as session
response = await session.get('http://example.org/users', params={'username': 'user1'})
self.assertEqual(response.status, 200)
users = await response.json()
self.assertEqual(len(users), 1)

with self.assertRaises(RouteNotFoundError)
with RouteManager() as rsps
rsps.add('GET', 'http://example.org/users?username=user1', json=[
{
'id': 1,
'username': 'user1',
'group': 'watchers',
},
], match_querystring=True)

with ClientSession() as session
await session.get('http://example.org/users')


许可证
=======

根据MIT许可证授权。


项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分布

testing-aiohttp-0.0.7.tar.gz (5.2 kB 查看哈希值)

上传时间 源代码

构建分布

testing_aiohttp-0.0.7-py2.py3-none-any.whl (8.9 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持