Bonus: Testing the Application

Now that you have finished the application and everything works as expected, it’s probably not a bad idea to add automated tests to simplify modifications in the future. The application above is used as a basic example of how to perform unit testing in the 1   Testing Flask Applications section of the documentation. Go there to see how easy it is to test Flask applications.

Adding Tests to flaskr

Assuming you have seen the testing section above and have either written your own tests for flaskr or have followed along with the examples provided, you might be wondering about ways to organize the project.

One possible and recommended project structure is:


For now go ahead a create the tests/ directory as well as the and files, if you haven’t already. The context file is used as an import helper. The contents of that file are:

import sys, os

basedir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, basedir + '/../')

from flaskr import flaskr

Testing + Setuptools

One way to handle testing is to integrate it with setuptools. All it requires is adding a couple of lines to the file and creating a new file setup.cfg. Go ahead and update the to contain:

from setuptools import setup


Now create setup.cfg in the project root (alongside


Now you can run:

python test

This calls on the alias created in setup.cfg which in turn runs pytest via pytest-runner, as the script has been called. (Recall the setup_requires argument in Following the standard rules of test-discovery your tests will be found, run, and hopefully pass.

This is one possible way to run and manage testing. Here pytest is used, but there are other options such as nose. Integrating testing with setuptools is convenient because it is not necessary to actually download pytest or any other testing framework one might use.