| 1 |
====================== |
| 2 |
Flask Test Integration |
| 3 |
====================== |
| 4 |
|
| 5 |
Integrating your `Flask`_ application with :pypi:`behave` is done via |
| 6 |
boilerplate code in your ``environment.py`` file. |
| 7 |
|
| 8 |
The `Flask documentation on testing`_ explains how to use the Werkzeug test |
| 9 |
client for running tests in general. |
| 10 |
|
| 11 |
.. _Flask: http://flask.pocoo.org/ |
| 12 |
.. _Flask documentation on testing: http://flask.pocoo.org/docs/latest/testing/ |
| 13 |
|
| 14 |
Integration Example |
| 15 |
=================== |
| 16 |
|
| 17 |
The example below is an integration boilerplate derived from the official |
| 18 |
Flask documentation, featuring the `Flaskr sample application`_ from the Flask |
| 19 |
tutorial. |
| 20 |
|
| 21 |
.. code-block:: python |
| 22 |
|
| 23 |
# -- FILE: features/environment.py |
| 24 |
import os |
| 25 |
import tempfile |
| 26 |
from behave import fixture, use_fixture |
| 27 |
# flaskr is the sample application we want to test |
| 28 |
from flaskr import app, init_db |
| 29 |
|
| 30 |
@fixture |
| 31 |
def flaskr_client(context, *args, **kwargs): |
| 32 |
context.db, app.config['DATABASE'] = tempfile.mkstemp() |
| 33 |
app.testing = True |
| 34 |
context.client = app.test_client() |
| 35 |
with app.app_context(): |
| 36 |
init_db() |
| 37 |
yield context.client |
| 38 |
# -- CLEANUP: |
| 39 |
os.close(context.db) |
| 40 |
os.unlink(app.config['DATABASE']) |
| 41 |
|
| 42 |
def before_feature(context, feature): |
| 43 |
# -- HINT: Recreate a new flaskr client before each feature is executed. |
| 44 |
use_fixture(flaskr_client, context) |
| 45 |
|
| 46 |
|
| 47 |
Taken and adapted from Ismail Dhorat's `BDD testing example on Flaskr`_. |
| 48 |
|
| 49 |
.. _Flaskr sample application: http://flask.pocoo.org/docs/latest/tutorial/introduction/ |
| 50 |
.. _BDD testing example on Flaskr: https://github.com/ismaild/flaskr-bdd |
| 51 |
|
| 52 |
|
| 53 |
Strategies and Tooling |
| 54 |
====================== |
| 55 |
|
| 56 |
See :ref:`id.practicaltips` for automation libraries and implementation tips |
| 57 |
on your BDD tests. |