File: usecase_flask.rst

package info (click to toggle)
behave 1.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,876 kB
  • sloc: python: 19,835; makefile: 136; sh: 18
file content (57 lines) | stat: -rw-r--r-- 1,782 bytes parent folder | download | duplicates (3)
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
======================
Flask Test Integration
======================

Integrating your `Flask`_ application with :pypi:`behave` is done via
boilerplate code in your ``environment.py`` file.

The `Flask documentation on testing`_ explains how to use the Werkzeug test
client for running tests in general.

.. _Flask: http://flask.pocoo.org/
.. _Flask documentation on testing: http://flask.pocoo.org/docs/latest/testing/

Integration Example
===================

The example below is an integration boilerplate derived from the official
Flask documentation, featuring the `Flaskr sample application`_ from the Flask
tutorial.

.. code-block:: python

    # -- FILE: features/environment.py
    import os
    import tempfile
    from behave import fixture, use_fixture
    # flaskr is the sample application we want to test
    from flaskr import app, init_db

    @fixture
    def flaskr_client(context, *args, **kwargs):
        context.db, app.config['DATABASE'] = tempfile.mkstemp()
        app.testing = True
        context.client = app.test_client()
        with app.app_context():
            init_db()
        yield context.client
        # -- CLEANUP:
        os.close(context.db)
        os.unlink(app.config['DATABASE'])

    def before_feature(context, feature):
        # -- HINT: Recreate a new flaskr client before each feature is executed.
        use_fixture(flaskr_client, context)


Taken and adapted from Ismail Dhorat's `BDD testing example on Flaskr`_.

.. _Flaskr sample application: http://flask.pocoo.org/docs/latest/tutorial/introduction/
.. _BDD testing example on Flaskr: https://github.com/ismaild/flaskr-bdd


Strategies and Tooling
======================

See :ref:`id.practicaltips` for automation libraries and implementation tips
on your BDD tests.