Skip to main content

No project description provided

Project description

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

Testing utilities for aiohttp. Python 3.5+ only.

Installation

$ pip install testing-aiohttp

Usage

rsps.MockRoutesTestCase

The MockRoutesTestCase will set up a mock application for mocking response.

The API is inspired by the responses library:

from aiohttp.test_utils import unittest_run_loop

from testing_aiohttp.rsps import MockRoutesTestCase


# Create your tests here.
class MyTestCase(MockRoutesTestCase):
    @unittest_run_loop
    async def test_response_data(self):
        with self.mock_response() as rsps:
            rsps.add('GET', '/users', data=[
                {
                    'id': 1,
                    'username': 'user1',
                    'group': 'watchers',
                },
                {
                    'id': 2,
                    'username': 'user2',
                    'group': 'watchers',
                },
            ])

            response = await self.client.get('/users')
            self.assertEqual(response, 200)
            users = await response.json()
            self.assertEqual(len(users), 2)
from aiohttp.test_utils import unittest_run_loop

from testing_aiohttp.rsps import MockRoutesTestCase


async def request_callback(request):
    return (200, {}, await request.text())


class MyTestCase(MockRoutesTestCase):

    @unittest_run_loop
    async def test_endpoint_detail_route(self):
        with self.mock_response() as rsps:
            rsps.add_callback(
                'POST', '/users/2/notify',
                callback=request_callback,
                content_type='application/json',
            )

            response = await self.generic_client.users(id=2).notify(unread=3)
            self.assertEqual(await response.json(), {'unread': 3})
from aiohttp.test_utils import unittest_run_loop

from testing_aiohttp.rsps import MockRoutesTestCase


class MyTestCase(MockRoutesTestCase):
    @unittest_run_loop
    async def test_response_match_querystring(self):
        with self.mock_response() as rsps:
            rsps.add('GET', '/users?username=user1', [
                {
                    'id': 1,
                    'username': 'user1',
                    'group': 'watchers',
                },
            ], match_querystring=True)

            response = await self.client.get('/users', params={'username': 'user1'})
            self.assertEqual(response.status, 200)
            users = await response.json()
            self.assertEqual(len(users), 1)

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

                await self.client.get('/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.5.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

testing_aiohttp-0.0.5-py2.py3-none-any.whl (7.3 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for testing-aiohttp-0.0.5.tar.gz
Algorithm Hash digest
SHA256 cf5fae2e5aaa6bb885ed8ce624d094838047c29632abe4ea1ef5c3cb0324456a
MD5 d327a8c464380d0a65664e68df6a4013
BLAKE2b-256 f47f7bb113dc93d15247983533a0b5c56c3f9ae006c2b9f3998a683894ac3581

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for testing_aiohttp-0.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e200f9b1a5133d4be02ab758d39f26e32898073e54ef0987dc2c159f437a6716
MD5 6c8e744f062d48e15e0af028a511f7da
BLAKE2b-256 0819bbd39e3a4340a527c8d89d4db55e407c67ab5b843bca281b1c9eab3b44f0

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