Flask Test Integration

Integrating your Flask application with 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.

Integration Example

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

# -- 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.

Strategies and Tooling

See Practical Tips on Testing for automation libraries and implementation tips on your BDD tests.