Chapter 8. Testing and Deploying an API with Flask

In this chapter, we will configure, write, and execute unit tests and learn a few things related to deployment. We will:

  • Set up unit tests
  • Create a database for testing
  • Write a first round of unit tests
  • Run unit tests and check testing coverage
  • Improve testing coverage
  • Understand strategies for deployments and scalability

Setting up unit tests

We will use nose2 to make it easier to discover and run unit tests. We will measure test coverage, and therefore, we will install the necessary package to allow us to run coverage with nose2. First, we will install the nose2 and cov-core packages in our virtual environment. The cov-core package will allow us to measure test coverage with nose2. Then, we will create a new PostgreSQL database that we will use for testing. Finally, we will create the configuration file for the testing environment.

Make sure you quit the Flask's development server. Remember that you just need to press Ctrl + C in the terminal or the Command Prompt window in which it is running. We just need to run the following command to install the nose2 package:

pip install nose2

The last lines of the output will indicate that the django-nose package has been successfully installed.

Collecting nose2
Collecting six>=1.1 (from nose2)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six, nose2
Successfully installed nose2-0.6.5 six-1.10.0

We just need to run the following command to install the cov-core package that will also install the coverage dependency:

pip install cov-core

The last lines for the output will indicate that the django-nose package has been successfully installed:

Collecting cov-core
Collecting coverage>=3.6 (from cov-core)
Installing collected packages: coverage, cov-core
Successfully installed cov-core-1.15.0 coverage-4.2

Now, we will create the PostgreSQL database that we will use as a repository for our testing environment. You will have to download and install a PostgreSQL database, in case you aren't already running it on the testing environment on your computer or in a testing server.

Tip

Remember to make sure that the PostgreSQL bin folder is included in the PATH environmental variable. You should be able to execute the psql command-line utility from your current Terminal or Command Prompt.

We will use the PostgreSQL command-line tools to create a new database named test_messages. In case you already have a PostgreSQL database with this name, make sure that you use another name in all the commands and configurations. You can perform the same task with any PostgreSQL GUI tool. In case you are developing on Linux, it is necessary to run the commands as the postgres user. Run the following command in macOS or Windows to create a new database named test_messages. Note that the command won't generate any output:

createdb test_messages

In Linux, run the following command to use the postgres user:

sudo -u postgres createdb test_messages

Now, we will use the psql command-line tool to run some SQL statements to grant privileges on the database to a user. In case you are using a different server than the development server, you will have to create the user before granting privileges. In macOS or Windows, run the following command to launch psql:

psql

In Linux, run the following command to use the postgres user

sudo -u psql

Then, run the following SQL statements and finally enter q to exit the psql command-line tool. Replace user_name with your desired user name to use in the new database and password with your chosen password. We will use the user name and password in the Flask testing configuration. You don't need to run the steps in case you are already working with a specific user in PostgreSQL and you have already granted privileges to the database for the user:

GRANT ALL PRIVILEGES ON DATABASE test_messages TO user_name;
q

Create a new test_config.py file within the api folder. The following lines show the code that declares variables that determine the configuration for Flask and SQLAlchemy for our testing environment. The SQL_ALCHEMY_DATABASE_URI variable generates a SQLAlchemy URI for the PostgreSQL database that we will use to run all the migrations before starting tests and we will drop all the elements after executing all the tests. Make sure you specify the desired test database name in the value for DB_NAME and that you configure the user, password, host, and port based on your PostgreSQL configuration for the testing environment. In case you followed the previous steps, use the settings specified in these steps. The code file for the sample is included in the restful_python_chapter_08_01 folder.

import os 
 
 
basedir = os.path.abspath(os.path.dirname(__file__)) 
DEBUG = True 
PORT = 5000 
HOST = "127.0.0.1" 
SQLALCHEMY_ECHO = False 
SQLALCHEMY_TRACK_MODIFICATIONS = True 
SQLALCHEMY_DATABASE_URI = "postgresql://{DB_USER}:{DB_PASS}@{DB_ADDR}/{DB_NAME}".format(DB_USER="user_name", DB_PASS="password", DB_ADDR="127.0.0.1", DB_NAME="test_messages") 
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') 
TESTING = True 
SERVER_NAME = '127.0.0.1:5000' 
PAGINATION_PAGE_SIZE = 5 
PAGINATION_PAGE_ARGUMENT_NAME = 'page' 
#Disable CSRF protection in the testing configuration 
WTF_CSRF_ENABLED = False 

As we did with the similar test file we created for our development environment, we will specify the previously created module as an argument to a function that will create a Flask app that we will use for testing. This way, we have one module that specifies all the values for the different configuration variables for our testing environment and another module that creates a Flask app for our testing environment. It is also possible to create a class hierarchy with one class for each environment we want to use. However, in our sample case, it is easier to create a new configuration file for our testing environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.248