| title | Flask counter with SQLite, SQLAlchemy, pytest | ||||
|---|---|---|---|---|---|
| timestamp | 2021-05-13 07:30:01 -0700 | ||||
| tags |
|
||||
| published | true | ||||
| books | flask |
||||
| author | szabgab | ||||
| archive | true | ||||
| show_related | true |
This is a counter example to show how to create a web application using Python Flask with SQLite database. Using SQLAlchemy as the ORM and ensuring that we can test the whole application using pytest. Making sure that each test-case has its own database.
Directory layout
.
├── app.py
├── model.py
├── templates
│ └── counter.html
└── test_app.py
To run the application execute the following:
FLASK_APP=app FLASK_DEBUG=1 flask run
The application
{% include file="examples/flask/sqlite-counter/app.py" %}
The model - the SQLAlchemy configuration
{% include file="examples/flask/sqlite-counter/model.py" %}
The HTML template using Jinja
{% include file="examples/flask/sqlite-counter/templates/counter.html" %}
The tests
The second test function was added primarily to show that the test functions have their own separate databases. and there is no interence between the test functions.
{% include file="examples/flask/sqlite-counter/test_app.py" %}
You can run the tests by
pytest