Skip to content

Commit 4f46bc9

Browse files
committed
Fix #1425
1 parent 8338295 commit 4f46bc9

File tree

1 file changed

+81
-18
lines changed

1 file changed

+81
-18
lines changed

docs/testing.rst

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,30 @@ Moreover we may break *backward compatibility* without *deprecation
4646
peroid* for some very strong reason.
4747

4848

49-
Pytest Example
50-
~~~~~~~~~~~~~~
49+
The Test Client and Servers
50+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
51+
52+
*aiohttp* test utils provides a scaffolding for testing aiohttp-based
53+
web servers.
54+
55+
They are consist of two parts: running test server and making HTTP
56+
requests to this server.
57+
58+
:class:`~aiohttp.test_utils.TestServer` runs :class:`aiohttp.web.Application`
59+
based server, :class:`~aiohttp.test_utils.RawTestServer` starts
60+
:class:`aiohttp.web.WebServer` low level server.
61+
62+
For performing HTTP requests to these servers you have to create a
63+
test client: :class:`~aiohttp.test_utils.TestClient` instance.
64+
65+
The client incapsulates :class:`aiohttp.ClientSession` by providing
66+
proxy methods to the client for common operations such as
67+
*ws_connect*, *get*, *post*, etc.
68+
69+
70+
71+
Pytest
72+
~~~~~~
5173

5274
The :data:`test_client` fixture available from pytest-aiohttp_ plugin
5375
allows you to create a client to make requests to test your app.
@@ -103,34 +125,75 @@ app test client::
103125
assert await resp.text() == 'value: bar'
104126

105127

106-
The Test Client and Servers
107-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
128+
Pytest tooling has the following fixtures:
108129

109-
*aiohttp* test utils provides a scaffolding for testing aiohttp-based
110-
web servers.
130+
.. data:: test_server(app, **kwargs)
111131

112-
They are consist of two parts: running test server and making HTTP
113-
requests to this server.
132+
A fixture factory that creates
133+
:class:`~aiohttp.test_utils.TestServer`::
114134

115-
:class:`~aiohttp.test_utils.TestServer` runs :class:`aiohttp.web.Application`
116-
based server, :class:`~aiohttp.test_utils.RawTestServer` starts
117-
:class:`aiohttp.web.WebServer` low level server.
135+
async def test_f(loop, test_server):
136+
app = web.Application(loop=loop)
137+
# fill route table
118138

119-
For performing HTTP requests to these servers you have to create a
120-
test client: :class:`aiohttp.test_utils.TestClient` instance.
139+
server = await test_server(app)
121140

122-
The client incapsulates :class:`aiohttp.ClientSession` by providing
123-
proxy methods to the client for common operations such as
124-
*ws_connect*, *get*, *post*, etc.
141+
The server will be destroyed on exit from test function.
142+
143+
*app* is the :class:`aiohttp.web.Application` used
144+
to start server.
145+
146+
*kwargs* are parameters passed to
147+
:meth:`aiohttp.web.Application.make_handler`
148+
149+
150+
.. data:: test_client(app, **kwargs)
151+
test_client(server, **kwargs)
152+
test_client(raw_server, **kwargs)
153+
154+
A fixture factory that creates
155+
:class:`~aiohttp.test_utils.TestClient` for access to tested server::
156+
157+
async def test_f(loop, test_client):
158+
app = web.Application(loop=loop)
159+
# fill route table
160+
161+
client = await test_client(app)
162+
resp = await client.get('/')
163+
164+
*client* and responses are cleaned up after test function finishing.
165+
166+
The fixture accepts :class:`aiohttp.web.Application`,
167+
:class:`aiohttp.test_utils.TestServer` or
168+
:class:`aiohttp.test_utils.RawTestServer` instance.
169+
170+
*kwargs* are parameters passed to
171+
:class:`aiohttp.test_utils.TestClient` constructor.
172+
173+
.. data:: raw_test_server(handler, **kwargs)
174+
175+
Af fixture factory that creates
176+
:class:`~aiohttp.test_utils.RawTestServer` instance from given web
177+
handler.
178+
179+
*handler* should be a coroutine which accepts a request and returns
180+
response, e.g.::
181+
182+
async def test_f(raw_test_server, test_client):
125183

184+
async def handler(request):
185+
return web.Response(text="OK")
126186

187+
raw_server = await raw_test_server(handler)
188+
client = await test_client(raw_server)
189+
resp = await client.get('/')
127190

128191
.. _aiohttp-testing-unittest-example:
129192

130193
.. _aiohttp-testing-unittest-style:
131194

132-
Unittest style
133-
~~~~~~~~~~~~~~
195+
Unittest
196+
~~~~~~~~
134197

135198
To test applications with the standard library's unittest or unittest-based
136199
functionality, the AioHTTPTestCase is provided::

0 commit comments

Comments
 (0)