aiohttp_tests
Unittest helper utilities for aiohttp-based applications.
Example usage:
import asyncio
import time
from unittest import mock
import aiohttp_tests as at
from aiohttp import web
settings = mock.Mock()
@at.override_settings(settings, SETTINGS_VALUE='new_value')
class ExampleTestCase(at.BaseTestCase):
""" Aiohttp tests utilities example"""
@asyncio.coroutine
def sample_view(self, request):
text = yield from self.sample_async_method()
return web.Response(text=text)
@asyncio.coroutine
def sample_async_method(self):
yield from asyncio.sleep(0.1)
return 'OK'
def init_app(self, loop):
""" You must initialize aiohttp application correctly."""
app = web.Application(loop=loop)
app.router.add_route('GET', '/', self.sample_view)
return app
def init_patchers(self):
super().init_patchers()
# Short alias for mock.patch that stores patcher and mock objects
# locally. Patchers are stopped automatically on teardown.
# Method is called by BaseTestCase.setUp
self.mock('time.time', return_value=self.now)
def setUp(self):
self.now = time.time()
super().setUp()
def testMockUtils(self):
""" Shows usage of get_mock and get_patcher methods."""
self.assertEqual(time.time(), self.now)
self.get_mock('time.time').return_value = self.now - 1
self.assertEqual(time.time(), self.now - 1)
self.get_patcher('time.time').stop()
self.assertGreater(time.time(), self.now)
def testOverrideSettings(self):
""" Django-style override_settings decorator for any settings module."""
self.assertEqual(settings.SETTINGS_VALUE, 'new_value')
with at.override_settings(settings, SETTINGS_VALUE='other_value'):
self.assertEqual(settings.SETTINGS_VALUE, 'other_value')
def testSyncClient(self):
""" Synchronous execution of requests, with new event loop every time.
Other HTTP methods, HTTP headers, request body or form/data are also
supported.
"""
response = self.client.get('/', headers={'Accept': 'application/json'})
self.assertEqual(response.status, 200)
self.assertEqual(response.text, 'OK')
self.assertEqual(response.headers.get('content-type'),
'text/plain; charset=utf-8')
# other HTTP methods
response = self.client.post('/', body='POST body')
self.assertEqual(response.status, 405)
# urlencoded form/data also supported
response = self.client.request('PUT', '/', data={'field': 'value'})
self.assertEqual(response.status, 405)
@at.async_test
def testAsyncClient(self):
""" test client requests could be done async, if needed."""
# you can mock any coroutine to return a 'done' Future with custom
# result.
done_future = self.empty_result("async response")
# mock.patck.object shortcut
self.mock_object(self, 'sample_async_method', return_value=done_future)
response = yield from self.client.get('/')
self.assertEqual(response.text, "async response")
Metadata
Release files for aiohttp_tests 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| aiohttp_tests-1.0.4.tar.gz | 4.5 kB | Without provenance | Details for aiohttp_tests-1.0.4.tar.gz |
Release files / aiohttp_tests-1.0.4.tar.gz
| Download URL | aiohttp_tests-1.0.4.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7574e36888161b9a42975c75c6db7baa4084fd249679907cffa3cbfd7facfa2f
|
|
BLAKE2b-256 checksum How to use checksums |
072fae88338a39d999b7736edfa312ccc3cc273a1519babc6dbcb895bd76611d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for aiohttp_tests 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.