Introduction to Flask unit testing
Flask unit testing is defined as a method of testing individual portions of source code that comprises one or more modules programmed together, which takes care of procedures of usage, procedures of operation, and the data associated with it to be tested to determine their fitness for usage. These tests are run by software developers so that one is ensured that the section of the code written for a specific task accomplishes the same along with meeting the design requirements and the behavior. These tests are typically automated tests targeted at individual function or procedures.
How to perform unit testing in Flask?
In order to perform the unit test, one must understand that in case a module doesn’t contain unit test cases, it is believed that the application either doesn’t run properly or has not been maintained as per coding standards. In short, there is a negative vibe if there is the unavailability of the unit testing piece. It is always followed that an environment needs to be created that makes the maintenance easy and eases out the import of smaller modules of one portion of the code into another application.
Below are few pointers which we need to keep in mind while we develop unit test frameworks or even perform unit testing:
- With the creation of a helper class, one can generate unit test cases quickly.
- The feedback on unit testing needs to be quick.
- The addition of test cases in the environment should be possible with ease.
- With a single command, one can execute the unit test cases.
- The path coverage’s feedback is provided visually.
Now, we will look at the most used unit test frameworks for python as mentioned below:
- unittest: This built-in framework is based on the xUnit framework in python.
- test: This module is used for building unit test cases.
Apart from these 2, there are an even much wider variety of unit testing tools available. Still, in order to respect the length of the article, we will restrict our discussion to the unittest, the most widely used case. Still, for information, we have tools like doctest, nose, testify, subunit, test tools, etc., which caters to individual cases as each of them has its own specialties. In the next few paragraphs, we will understand the process of performing the unit tests in Flask.
1. Storing the Unit tests
Though the flexibility is given to store the unit test case files in any location of the system but as a standard practice, it is always mandated to store the unit tests in the ‘tests’ directory available within the project. It is available at the same level as the files which are to be tested are stored. For example, in the below tree, we see that the ‘tests’ folder is present in the same location as the one where the components to be tested are placed, and within the ‘tests’ folder, all the unit tests are present.
│ ├── test_ABC.py
│ ├── test_DEF.py
│ └── test_users.py
2. Creation of the basic unit test file
Once we have followed the steps for storing unit cases, and before we even start writing unit test cases, it is very important for us to build an environment that will enable smooth writing of test cases. This environment consists of ‘helper’ functions that facilitate the writing of unit test cases. This unit test case file consists of a class, and within the class, various methods are defined, which serves the purpose of testing components of the code. We need to include some mandatory unit case methods like setup(), which is called before any unit test case runs. Methods named as teardown() are executed post finish of unit test case execution.
3. Running of Unit Tests
Now that we have created the basic unit test file let us try out running the just created file. This is to ensure that we do some sanity run checks with the environment created before we start building on more functions to facilitate more coverage. The command to run the same is python <python file name.py>. One thing to keep in mind is to use the top-level folder of the flask application. We might need to import app, db like objects and hence would need to specify a location that is easily discoverable, and python interpreter can locate it easily.
4. Addition of more helper function for facilitating more unit test development
Now, as per the requirement of the type of functionality, we would like to test for helper functions that are added, which checks for the required functionalities and then return a positive or negative confirmation.
5. Code coverage
In order to get a Flask function that will determine the coverage, we recommend using the module named coverage, which can be installed by executing pip install coverage. For running the corresponding coverage command, we use coverage run <python file name>, and once the data is collected, we would run the report command by executing coverage report <python file name>
Examples of Flask unit testing
Here are the following examples mention below
Setting up unit test cases to check the sum of the first 5 numbers:
import unittest class SumofFirst5Numbers(unittest.TestCase): def test_sum(self): self.assertEqual(sum([1, 2, 3, 4, 5]), 15, "Should be 6") def test_sum_tuple(self): self.assertEqual(sum((1, 2, 2, 4, 5)), 10, "Should be 6") if __name__ == '__main__': unittest.main()
Here, one case passed, and the other failed because the sum of the numbers provided is not 15.
Installation of coverage module:
pip install coverage
The requirement of unit testing in Flask is equally important because of various moving parts in a Flask application, and individual parts should perform their task for the harmonic functioning of the application. In this article, we have understood a wide variety of ways by which one can perform unit testing in Flask. Rest is left to the readers to start your experimentation to the baseline created here!
This is a guide to Flask unit testing. Here we discuss How to perform unit testing in Flask along with the examples and outputs. You may also have a look at the following articles to learn more –