Basic

The app is one object

A Flask app is an object that holds the routes and the state. One object is the reason a test can build its own app instead of reaching the one that is running.

What you will see

2. The list on the object is the contract, and the contract is printed.

About 1 min of reading

Try It

Build two apps and prove they differ

1

Write the factory

printf 'def make_app(settings): return App(settings)\n' > /tmp/f.py; grep -c def /tmp/f.py

Before you run it, say out loud what it will print. Then open the answer.

What it prints
1. One function creates the app, which is the shape.
2

Build two with different settings

printf 'a debug=on\nb debug=off\n' > /tmp/a.txt; awk '{print $2}' /tmp/a.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
on then off. Two apps, two settings, and neither reaches the other.
3

Print the route list

printf '/health\n/api/ping\n' > /tmp/r.txt; wc -l < /tmp/r.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2. The list on the object is the contract, and the contract is printed.

Data Flow

A module to a factory
1 The app is created in a function
↓
2 The settings are passed in
↓
3 The routes are registered on the object
↓
4 The test builds its own app
Why this works
Why the factory is the shape

A test that builds an app is a test that owns its app. A test that reaches a global is a test that depends on import order.

The one symptom of a global app

Two tests that change the same setting. The change is visible in the other test, and the cause is the shared object.

Examples

The app is created, not found

A module level app is a global. A function that returns an app is a factory, and a factory can be built twice.

Config is given, not read

The app takes its settings at creation. Settings read from the environment inside a handler are settings that change mid run.

One object is one contract

The routes are a list on the object. The list can be printed, and a printed list is a contract.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
A route is a registration
A route is a registration →