The Flask-Testing extension provides unit testing utilities for Flask.
Source code and issue tracking at Bitbucket.
Install with pip and easy_install:
pip install Flask-Testing
or download the latest version from Bitbucket:
hg clone http://bitbucket.org/danjac/flask-testing
cd flask-testing
python setup.py develop
If you are using virtualenv, it is assumed that you are installing Flask-Testing in the same virtualenv as your Flask application(s).
Simply subclass the TestCase class:
from flaskext.testing import TestCase
class MyTest(TestCase):
pass
You must specify the create_app method, which should return a Flask instance:
from flaskext.testing import TestCase
class MyTest(TestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
return app
If you don’t define create_app a NotImplementedError will be raised.
If you are testing a view that returns a JSON response, you can test the output using a special json attribute appended to the Response object:
@app.route("/ajax/")
def some_json():
return jsonify(success=True)
class TestViews(TestCase):
def test_some_json(self):
response = self.client.get("/ajax/")
self.assertEquals(response.json, dict(success=True))
Twill is a simple language for browing the Web through a command line interface. You can use it in conjunction with TwillTestCase to write functional tests for your views.
TwillTestCase is a subclass of TestCase. It sets up Twill for use with your test cases. See the API below for details.
This covers a couple of points if you are using Flask-Testing with SQLAlchemy. It is assumed that you are using the Flask-SQLAlchemy extension, but if not the examples should not be too difficult to adapt to your own particular setup.
First, ensure you set the database URI to something other than your production database ! Second, it’s usually a good idea to create and drop your tables with each test run, to ensure clean tests:
from flaskext.testing import TestCase
from myapp import create_app, db
class MyTest(TestCase):
SQLALCHEMY_DATABASE_URI = "sqlite://"
TESTING = True
def create_app(self):
# pass in test configuration
return create_app(self)
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
Notice also that db.session.remove() is called at the end of each test, to ensure the SQLAlchemy session is properly removed and that a new session is started with each test run - this is a common “gotcha”.
Also notice that for this example the SQLite in-memory database is used : while it is faster for tests, if you have database-specific code (e.g. for MySQL or PostgreSQL) it may not be applicable.
You may also want to add a set of instances for your database inside of a setUp() once your database tables have been created. If you want to work with larger sets of data, look at Fixture which includes support for SQLAlchemy.
Checks if response status code is 200
Parameters: |
|
---|
Checks if response status code is 403
Parameters: |
|
---|
Checks if response status code is 404
Parameters: |
|
---|
Checks if response status code is 405
Parameters: |
|
---|
Checks if given name exists in the template context and equals the given value.
:param name:name of context variable :param value: value to check against
Checks if response is an HTTP redirect to the given location.
Parameters: |
|
---|
Helper method to check matching response status.
Parameters: |
|
---|
Checks if a given template is used in the request. Only works if your version of Flask has signals support (0.6+) and blinker is installed.
Parameters: |
|
---|
Checks if response status code is 200
Parameters: |
|
---|
Checks if response status code is 403
Parameters: |
|
---|
Checks if response status code is 404
Parameters: |
|
---|
Checks if response status code is 405
Parameters: |
|
---|
Checks if given name exists in the template context and equals the given value.
:param name:name of context variable :param value: value to check against
Checks if response is an HTTP redirect to the given location.
Parameters: |
|
---|
Helper method to check matching response status.
Parameters: |
|
---|
Checks if a given template is used in the request. Only works if your version of Flask has signals support (0.6+) and blinker is installed.
Parameters: |
|
---|
Create your Flask app here, with any configuration you need.
Returns a variable from the context passed to the template. Only works if your version of Flask has signals support (0.6+) and blinker is installed.
Raises a ContextVariableDoesNotExist exception if does not exist in context.
Parameters: |
|
---|