test_utils

Utilities shared by tests.

Module Contents

Classes

BaseTestServer(self,**kwargs)
TestServer(self,app,**kwargs)
RawTestServer(self,handler,**kwargs)
TestClient(self,server,**kwargs) A test client implementation.
AioHTTPTestCase() A base class to allow for unittest web applications using

Functions

unused_port() Return a port that is unused on the current host.
unittest_run_loop(func,*args,**kwargs) A decorator dedicated to use with asynchronous methods of an
loop_context(loop_factory=None,fast=False) A contextmanager that creates an event_loop, for test purposes.
setup_test_loop(loop_factory=None) Create and return an asyncio.BaseEventLoop
teardown_test_loop(loop,fast=False) Teardown and cleanup an event_loop created
_create_app_mock()
_create_transport(sslcontext=None)
make_mocked_request(method,path,headers=None) Creates mocked web.Request testing purposes.
make_mocked_coro(return_value=sentinel,raise_exception=sentinel) Creates a coroutine mock.
unused_port()

Return a port that is unused on the current host.

class BaseTestServer(**kwargs)
__init__(**kwargs)
start_server(loop=None, **kwargs)
_make_runner(**kwargs)
make_url(path)
started()
closed()
handler()
close()

Close all fixtures created by the test client.

After that point, the TestClient is no longer usable.

This is an idempotent function: running close multiple times will not have any additional effects.

close is also run when the object is garbage collected, and on exit when used as a context manager.

__enter__()
__exit__(exc_type, exc_value, traceback)
__aenter__()
__aexit__(exc_type, exc_value, traceback)
class TestServer(app, **kwargs)
__init__(app, **kwargs)
_make_runner(**kwargs)
class RawTestServer(handler, **kwargs)
__init__(handler, **kwargs)
_make_runner(debug=True, **kwargs)
class TestClient(server, **kwargs)

A test client implementation.

To write functional tests for aiohttp based servers.

__init__(server, **kwargs)
start_server()
host()
port()
server()
app()
session()

An internal aiohttp.ClientSession.

Unlike the methods on the TestClient, client session requests do not automatically include the host in the url queried, and will require an absolute path to the resource.

make_url(path)
request(method, path, *args, **kwargs)

Routes a request to tested http server.

The interface is identical to asyncio.ClientSession.request, except the loop kwarg is overridden by the instance used by the test server.

get(path, *args, **kwargs)

Perform an HTTP GET request.

post(path, *args, **kwargs)

Perform an HTTP POST request.

options(path, *args, **kwargs)

Perform an HTTP OPTIONS request.

head(path, *args, **kwargs)

Perform an HTTP HEAD request.

put(path, *args, **kwargs)

Perform an HTTP PUT request.

patch(path, *args, **kwargs)

Perform an HTTP PATCH request.

delete(path, *args, **kwargs)

Perform an HTTP PATCH request.

ws_connect(path, *args, **kwargs)

Initiate websocket connection.

The api corresponds to aiohttp.ClientSession.ws_connect.

_ws_connect(path, *args, **kwargs)
close()

Close all fixtures created by the test client.

After that point, the TestClient is no longer usable.

This is an idempotent function: running close multiple times will not have any additional effects.

close is also run on exit when used as a(n) (asynchronous) context manager.

__enter__()
__exit__(exc_type, exc_value, traceback)
__aenter__()
__aexit__(exc_type, exc_value, traceback)
class AioHTTPTestCase

A base class to allow for unittest web applications using aiohttp.

Provides the following:

  • self.client (aiohttp.test_utils.TestClient): an aiohttp test client.
  • self.loop (asyncio.BaseEventLoop): the event loop in which the
    application and server are running.
  • self.app (aiohttp.web.Application): the application returned by
    self.get_application()

Note that the TestClient’s methods are asynchronous: you have to execute function on the test client using asynchronous methods.

get_application()

This method should be overridden to return the aiohttp.web.Application object to test.

get_app()

Obsolete method used to constructing web application.

Use .get_application() coroutine instead

setUp()
setUpAsync()
tearDown()
tearDownAsync()
get_server(app)

Return a TestServer instance.

get_client(server)

Return a TestClient instance.

unittest_run_loop(func, *args, **kwargs)

A decorator dedicated to use with asynchronous methods of an AioHTTPTestCase.

Handles executing an asynchronous function, using the self.loop of the AioHTTPTestCase.

loop_context(loop_factory=None, fast=False)

A contextmanager that creates an event_loop, for test purposes.

Handles the creation and cleanup of a test loop.

setup_test_loop(loop_factory=None)

Create and return an asyncio.BaseEventLoop instance.

The caller should also call teardown_test_loop, once they are done with the loop.

teardown_test_loop(loop, fast=False)

Teardown and cleanup an event_loop created by setup_test_loop.

_create_app_mock()
_create_transport(sslcontext=None)
make_mocked_request(method, path, headers=None)

Creates mocked web.Request testing purposes.

Useful in unit tests, when spinning full web server is overkill or specific conditions and errors are hard to trigger.

make_mocked_coro(return_value=sentinel, raise_exception=sentinel)

Creates a coroutine mock.