Skip to main content

No project description provided

Project description

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

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

Testing utilities for `aiohttp`. Python 3.5+ only.


Installation
============

::

$ pip install testing-aiohttp

Usage
=====

``AioLoopTestCase``
-------------------

A ``TestCase`` subclass to provides an asyncio loop, so that it can be used in conjuction with ``aiohttp.test_utils.unittest_run_loop``::

from aiohttp.test_utils import unittest_run_loop

from test_aiohttp import AioLoopTestCase

async def add(a, b):
return a + b

# Create your tests here.
class MyTestCase(AioLoopTestCase):
@unittest_run_loop
async def test_something_async(self):
my_result = await add(1, 2)
self.assertEqual(my_result, 3)

For convenience, it also provides ``setUpAsync`` and a ``tearDownAsync`` methods.

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

``RouteManager`` will mock up responses for `aiohttp.Client``.

The API is inspired by the ``responses`` library::

from aiohttp import ClientSession
from aiohttp.test_utils import unittest_run_loop

from testing_aiohttp import AioLoopTestCase, RouteManager


# Create your tests here.
class MyTestCase(AioLoopTestCase):
@unittest_run_loop
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 aiohttp.test_utils import unittest_run_loop

from testing_aiohttp import AioLoopTestCase, RouteManager


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


class MyTestCase(AioLoopTestCase):

@unittest_run_loop
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 aiohttp.test_utils import unittest_run_loop

from testing_aiohttp.rsps import AioLoopTestCase, RouteManager, RouteNotFoundError


class MyTestCase(AioLoopTestCase):
@unittest_run_loop
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')


License
=======

Licensed under the MIT License.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testing-aiohttp-0.0.6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

testing_aiohttp-0.0.6-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file testing-aiohttp-0.0.6.tar.gz.

File metadata

File hashes

Hashes for testing-aiohttp-0.0.6.tar.gz
Algorithm Hash digest
SHA256 b02364903efc5e1ee2c9df08caf8f9782a4c737ff48e5dbff5f73314bbcda331
MD5 3266864494f2ea1f5485a7ffc6266189
BLAKE2b-256 f4a8a70228ee5a4b73342634d6cd8e1b76d74be6b1de8c69351aee6245f864e6

See more details on using hashes here.

Provenance

File details

Details for the file testing_aiohttp-0.0.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for testing_aiohttp-0.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2cd146cc98290e694f56a491627220c13b87598a072edd22bca52f5d9d9fc459
MD5 41285d55a20724f5f8b01db3a4c398d8
BLAKE2b-256 c9550b344c5480410ff1e641eca801e6b61dfba652d22e5c430958ec0b89e9e1

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page