Create a mock application with Flask

Create mock apps with Flask

At the start of a new project, the testing is very easy. The software guys create a basic application that you have to test. Most of the times it contains a kind of hello world. In that starting phase I create a mock application with Flask to test against.

I create the mock application because of the rapid development of tests. I first determine the API together with the developers. Afterwards I create my mock application.

Installation of dependencies

Logo of Flask-RESTful

Most of the time I use Flask and Flask-RESTful to create a server in seconds. First I install the libraries in my virtual environment. In my post to create a python test environment I explain how to create a virtual environment in python. In this environment you can install the dependencies with pip as follows:

pip install flask
pip install flask-restful

These commands installs the dependencies. It is time to determine what I have to test. Suppose that the application has some basic API to fetch some users if you have a user id. The API is as follows:

The mock application

GET  /person/<ID> 

The reply of the application is in this case the following reply:

  "id": 1,
  "name": "Anne Bonny",
  "profession": "Pirate"

Then you can create the mock application with Flask in a few lines of code.

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

persons = [
    dict(id=1, name="Anne Bonny", profession="Pirate")

class PersonResource(Resource):
    def get(self, id):
        return persons[id]

api.add_resource(PersonResource, '/person/<int:id>')

if __name__ == '__main__':

In Flask-RESTful the building blocks are resources. A resource gives easy access to HTTP methods by defining the functions on the resource. In my example I have only a GET method that returns a dict with the reply data.

The result

This little application can run like every other program. The program prints some debugging information on the console window like this.

$ python ./ 
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 505-170-893

The console window shows that the server is running on port 5000. I can now test the mock server with curl. In this way I see the following reply in another console window.

$ curl
    "id": 1,
    "name": "Anne Bonny",
    "profession": "Pirate"

Creating a mock application with Flask is very easy and does not need a lot of code. There are some advantages to create a mock application.

  • Testers can create their API tests. At the same moment the developers create the application.
  • If the tests with the real application fails, testers talk with the developers. There is a conversation about the API.

A mock application is one of my useful test tools. Try it to see if it is a benefit for your situation. In my case it is.