File: features.rst

package info (click to toggle)
pytest-flask 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: python: 553; makefile: 161; sh: 5
file content (368 lines) | stat: -rw-r--r-- 10,880 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
.. _features:

Feature reference
=================

Extension provides some sugar for your tests, such as:

* Access to context bound objects (``url_for``, ``request``, ``session``)
  without context managers:

  .. code:: python

    def test_app(client):
        assert client.get(url_for('myview')).status_code == 200

* Easy access to ``JSON`` data in response:

  .. code:: python

    @api.route('/ping')
    def ping():
        return jsonify(ping='pong')

    def test_api_ping(client):
        res = client.get(url_for('api.ping'))
        assert res.json == {'ping': 'pong'}

  .. note::

    User-defined ``json`` attribute/method in application response class will
    not be overwritten. So you can define your own response deserialization method:

    .. code:: python

        from flask import Response
        from myapp import create_app

        class MyResponse(Response):
            '''Implements custom deserialization method for response objects.'''
            @property
            def json(self):
                return 42

        @pytest.fixture(scope="session")
        def app():
            app = create_app()
            app.response_class = MyResponse
            return app

        def test_my_json_response(client):
            res = client.get(url_for('api.ping'))
            assert res.json == 42

* Running tests in parallel with `pytest-xdist`_. This can lead to
  significant speed improvements on multi core/multi CPU machines.

  This requires the ``pytest-xdist`` plugin to be available, it can usually be
  installed with::

    pip install pytest-xdist

  You can then run the tests by running::

    pytest -n <number of processes>

**Not enough pros?** See the full list of available fixtures and markers
below.


Fixtures
--------

``pytest-flask`` provides a list of useful fixtures to simplify application
testing. More information on fixtures and their usage is available in the
`pytest documentation`_.


``client`` - application test client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

An instance of ``app.test_client``. Typically refers to
`flask.Flask.test_client`_.

.. hint::

    During test execution a request context will be automatically pushed
    for you, so context-bound methods can be conveniently called (e.g.
    ``url_for``, ``session``.

Example:

.. code:: python

    def test_myview(client):
        assert client.get(url_for('myview')).status_code == 200


``client_class`` - application test client for class-based tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example:

.. code:: python

    @pytest.mark.usefixtures('client_class')
    class TestSuite:

        def test_myview(self):
            assert self.client.get(url_for('myview')).status_code == 200


``config`` - application config
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

An instance of ``app.config``. Typically refers to `flask.Config`_.


``live_server`` - application live server
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Run application in a separate process (useful for tests with Selenium_ and
other headless browsers).

.. hint::

    The server’s URL can be retrieved using the ``url_for`` function.

.. code:: python

    from flask import url_for

    @pytest.mark.usefixtures('live_server')
    class TestLiveServer:

        def test_server_is_up_and_running(self):
            res = urllib2.urlopen(url_for('index', _external=True))
            assert b'OK' in res.read()
            assert res.code == 200


``--start-live-server`` - start live server automatically (default)
```````````````````````````````````````````````````````````````````


``--no-start-live-server`` - don’t start live server automatically
``````````````````````````````````````````````````````````````````

By default the server will start automatically whenever you reference
``live_server`` fixture in your tests. But starting live server imposes some
high costs on tests that need it when they may not be ready yet. To prevent
that behaviour pass ``--no-start-live-server`` into your default options (for
example, in your project’s ``pytest.ini`` file)::

    [pytest]
    addopts = --no-start-live-server

.. note::

    You **should manually start** live server after you finish your application
    configuration and define all required routes:

    .. code:: python

        def test_add_endpoint_to_live_server(live_server):
            @live_server.app.route('/test-endpoint')
            def test_endpoint():
                return 'got it', 200

            live_server.start()

            res = urlopen(url_for('test_endpoint', _external=True))
            assert res.code == 200
            assert b'got it' in res.read()


``--live-server-wait`` - the live server wait timeout (5 seconds)
`````````````````````````````````````````````````````````````````
The timeout after which test case is aborted if live server is not started.


``--live-server-port`` - use a fixed port
`````````````````````````````````````````
By default the server uses a random port. In some cases it is desirable to run
the server with a fixed port. You can use ``--live-server-port`` (for example,
in your project's ``pytest.ini`` file)::

    [pytest]
    addopts = --live-server-port=5000


``live_server_scope`` - set the scope of the live server
``````````````````````````````````````````````````````````````````

By default, the server will be scoped to ``session`` for performance reasons, however
if your server has global state and you want better test isolation, you can use the
``live_server_scope`` ini option to change the fixture scope:

.. code-block:: ini

    [pytest]
    live_server_scope = function


HTTP Request
~~~~~~~~~~~~~~~~~~~

Common request methods are available through the internals of the `Flask API`_.
Specifically, the API creates the default `flask.Flask.test_client`_ instance,
which works like a regular `Werkzeug test client`_.

Examples:

.. code:: python

    def test_post_request(client, live_server):
        @live_server.app.route('/load-data')
        def get_endpoint():
            return url_for('name.load', _external=True)

        live_server.start()

        res = client.post(
            get_endpoint(),
            headers={'Content-Type': 'application/json'},
            data={}
        )

        assert res.status_code == 200

.. code:: python

    def test_get_request(client, live_server):
        @live_server.app.route('/load-data')
        def get_endpoint():
            return url_for('name.load', _external=True)

        live_server.start()

        res = client.get(get_endpoint())

        assert res.status_code == 200

.. note::

    The notation ``name.load_data``, corresponds to a ``endpoint='load'``
    attribute, within a route decorator. The following is a route decorator
    using the `blueprint`_ implementation:

        .. code:: python

            from flask import Blueprint, request

            # local variables
            blueprint = Blueprint(
                'name',
                __name__,
                template_folder='interface/templates',
                static_folder='interface/static'
            )

            @blueprint.route('/load-data', methods=['POST'], endpoint='load')
            def load_data():
                if request.method == 'POST':
                    if request.get_json():
                        pass

Alternatively, the route function can be referenced directly from the
``live_server`` implementation, rather than implementing an ``endpoint``:

    .. code:: python

        def test_load_data(live_server, client):
            @live_server.app.route('/load-data', methods=['POST'])
            def load_data():
                pass

            live_server.start()

            res = client.post(url_for('load_data'), data={})
            assert res.status_code == 200

.. note::

    Remember to explicitly define which ``methods`` are supported when
    registering the above route function.


Content negotiation
~~~~~~~~~~~~~~~~~~~

An important part of any :abbr:`REST (REpresentational State Transfer)`
service is content negotiation. It allows you to implement behaviour such as
selecting a different serialization schemes for different media types.

    HTTP has provisions for several mechanisms for "content negotiation" - the
    process of selecting the best representation for a given response
    when there are multiple representations available.

    -- :rfc:`2616#section-12`. Fielding, et al.

The most common way to select one of the multiple possible representation is
via ``Accept`` request header. The following series of ``accept_*`` fixtures
provides an easy way to test content negotiation in your application:

.. code:: python

    def test_api_endpoint(accept_json, client):
        res = client.get(url_for('api.endpoint'), headers=accept_json)
        assert res.mimetype == 'application/json'


``accept_any`` - :mimetype:`*/*` accept header
``````````````````````````````````````````````

:mimetype:`*/*` accept header suitable to use as parameter in ``client``.


``accept_json`` - :mimetype:`application/json` accept header
````````````````````````````````````````````````````````````

:mimetype:`application/json` accept header suitable to use as parameter in
``client``.


``accept_jsonp`` - :mimetype:`application/json-p` accept header
```````````````````````````````````````````````````````````````

:mimetype:`application/json-p` accept header suitable to use as parameter in
``client``.


Markers
-------

``pytest-flask`` registers the following markers. See the pytest documentation
on `what markers are`_ and for notes on `using them`_.


``pytest.mark.options`` - pass options to your application config
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. py:function:: pytest.mark.options(**kwargs)

   The mark used to pass options to your application config.

   :type kwargs: dict
   :param kwargs:
     The dictionary used to extend application config.

   Example usage:

   .. code:: python

       @pytest.mark.options(debug=False)
       def test_app(app):
           assert not app.debug, 'Ensure the app is not in debug mode'


.. _pytest-xdist: https://pypi.org/project/pytest-xdist/
.. _pytest documentation: https://pytest.org/en/latest/fixture.html
.. _flask.Flask.test_client: https://flask.palletsprojects.com/api/#flask.Flask.test_client
.. _flask.Config: https://flask.palletsprojects.com/api/#flask.Config
.. _Selenium: https://selenium-python.readthedocs.io/
.. _what markers are: https://pytest.org/en/latest/mark.html
.. _using them: https://pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
.. _Flask API: https://flask.palletsprojects.com/api/
.. _Werkzeug test client: https://werkzeug.palletsprojects.com/test/#werkzeug.test.Client
.. _blueprint: https://flask.palletsprojects.com/blueprints/