This video is available to students only

How to Configure a Flask App for Unit Testing With PyTest


One of the most important ways to ensure that your Flask application is maintainable is to create automated tests to ensure key parts of the application work. Many developers believe that writing automated tests slows down the pace of development, but when working on large applications, especially when you have multiple team members working on the same code base, tests allow you to move faster because you can confidently make changes without having to manually test features.

One common issue that emerges in untested applications is the inability to handle updates in dependencies or libraries. If a new version of Python or Flask comes out with security updates, you'd have to manually test all parts of the application to be able to confidently upgrade versions. If you multiply the effort required to do that manual testing with the amount of times that an upgrade is necessary or you make changes to your application and then compare it with the time required to have set up automated tests, you'll wish that you had just started with writing automated tests.

Fortunately writing tests with Flask can be easy to set up and actually can be fun to write with the right setup. In this chapter we'll focus on unit testing and some amount of integration testing as well. As we continue to build out Flask applications in later chapters, we will continue to build out a test suite. The tests suites we will build will aim for 100% code coverage, but just because a test suite hits every line of our application, it still does not offer a guarantee against bugs.

Configuring Flask for PyTest

PyTest is a framework for writing tests. It allows us to write tests as functions that use assert statements to define what the expected behavior of our application code is. In addition, PyTest also provides a test runner that discovers the tests defined in our application and a way to run all tests from the command line.

PyTest is a generic library that's usable for any Python project. It's often the case that popular projects will have extensions specifically designed to make using them with Flask easier. In our case, a library called pytest-flask exists that simplifies the setup involved for using PyTest and Flask together.

To add PyTest and pytest-flask into our application, add those two libraries as dependencies in the requirements.txt file. At this point, your requirements.txt file should look like this:



Virtual Environment Reminder

In your terminal you should see your command line prefixed with (env) to indicate that you are using a virtual environment.

If you aren't and want to use one, then you'll want to activate it with source env/bin/activate

Now you can install those new libraries by pointing pip to the requirements.txt file.

pip install -r requirements.txt

Similar to how we tell Flask where to find our application (through the use of the FLASK_APP=stock_app:create_app('dev') environment variable), we will need to tell PyTest how to find and set up our Flask app. We will want a copy of our application to be set up for almost all of our test suites and instead of duplicating the code to set up an application, we can define a single function that each test suite will run before hand. PyTest calls these shared functions fixtures. When we define a test, we list the name of the fixture we'd like to use as a parameter of the test function.

Start a new discussion. All notification go to the author.