A set of py.test fixtures to test Flask applications.
Project description
A set of pytest fixtures to test Flask extensions and applications.
Features
Plugin provides some fixtures to simplify app testing:
client - an instance of app.test_client,
client_class - client fixture for class-based tests,
config - you application config,
live_server - runs an application in the background (useful for tests with Selenium and other headless browsers),
accept_json, accept_jsonp, accept_any - accept headers suitable to use as parameters in client.
To pass options to your application use the pytest.mark.app marker:
@pytest.mark.app(debug=False)
def test_app(app):
assert not app.debug, 'Ensure the app not in debug mode'
During tests execution the application has pushed context, e.g. url_for, session and other context bound objects are available without context managers:
def test_app(client):
assert client.get(url_for('myview')).status_code == 200
Response object has a json property to test a view that returns a JSON response:
@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'}
If you want your tests done via Selenium or other headless browser use the live_server fixture. The server’s URL can be retrieved using the url_for function:
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
Quick Start
To start using a plugin define your application fixture in conftest.py:
from myapp import create_app
@pytest.fixture
def app():
app = create_app()
return app
Install the extension with dependencies and run your test suite:
$ pip install pytest-flask
$ py.test
Documentation
The latest documentation is available at http://pytest-flask.readthedocs.org/en/latest/.
Contributing
Don’t hesitate to create a GitHub issue for any bug or suggestion.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_flask-0.7.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1c1a2a7b78f6f00bef51dbe395897fac8324e3e997b91dcc59efa547ca966ce |
|
MD5 | 270fa6e0f3489d304cd09d7c1cad5ada |
|
BLAKE2b-256 | 7eefc8ecd7381a25d2206ddbed2f95a15471c48a8ab3a4022cf92c9b1298baf9 |