Flask Series: Testing

New code, everything that might break or does not work correctly should be tested. 100% code coverage does not guarantee a working system, but still you have to try to implement as much or more test code as production one.

There are different types of testing, but I will concentrate on unit testing.

Unit Testing
Unit test is a piece of code that tests a unit of work, logical unit in the tested system. Your unit tests should be:
– automated;
– independent;
– consistent and repeatable;
– maintainable;

Python Testing Frameworks
– others;

unittest is the built-in testing module for implementing unit tests in Python, it is an xUnit framework and shares some important components:
– test runner – executes the tests and provides the test results to the user;
– test case – smallest unit of testing;
– test fixture – preparation needed for test cases execution;
– test suite – set of tests cases based on some criteria;
– test execution;
– assertions – function that verifies some state or behavior;

Test Discovery 
You could use the unittest support for test discovery to run your unit tests:

cd project_directory
python -m unittest discover

More information on the unittest discovery functionality can be found here.

To verify if your code is working correcly you have to use the assert methods, provided by the TestClass:
– assertEqual;
– assertNotEqual;
– assertTrue;
– etc.;

Complete list with assert statements.

nose extends the unittest framework. It is not built-in like unittest, that’s why you should install it:

pip install nose

Together with the nose package and nosetests script will be installed on your machine, used to discover and run tests.

cd project_directory

More information on nose can be found here.

How to Test a Flask Application?

Flask exposes the Werkzeug test client and handles the context locals. It is important to set TESTING configuration setting to true, to propagate the exceptions to the test client.

Helpful information how to test systems that requires logins can be found in these tests.

Session can be asserted for given keys and values in the following way:

Further tips and tricks on testing Flask applications can found here.

This help article section provides additional information on Flask session testing.

Flask-Testing Extension
Flask-Testing is very helpful Flask extension, that make Flask developers testing efforts easier.


pip install Flask-Testing

Example Tests:

Test Doubles
This is a concept used to describe a scenario where you replace your production object with a testing one, that can be easily configured, manipulated and asserted (if needed).
– Fake;
– Stubs;
– Mocks;
– etc.

I can suggest you the term explanation provided by Martin Fowler in his bliki article about Test Doubles.

Mocking is very important in testing scenarios, in order to avoid external dependencies in your code, like service calls, I/O operations, etc.

In the flask-bookshelf I will use mock framework.

Code Coverage
Code Coverage in Wikipedia: code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite.

To measure the code coverage in the Flask application I will use coverage.

pip install coverage

Now that you have it installed, to get the needed measurement data you have to run the following command:

coverage run –source=app_folder_path

To print the prepared report execute:

coverage report 

The complete demo application, described in this blog post, can be found here.

In the next blog post I will describe how to work with views and web forms in your Flask application.