https://github.com/alkc/flask-mongomock-example
Example of using mongomock for testing in a global mongo db class in a flask app
https://github.com/alkc/flask-mongomock-example
flask mongodb mongomock pytest pytest-mock python
Last synced: about 2 months ago
JSON representation
Example of using mongomock for testing in a global mongo db class in a flask app
- Host: GitHub
- URL: https://github.com/alkc/flask-mongomock-example
- Owner: alkc
- Created: 2023-05-24T07:23:25.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-24T13:19:02.000Z (about 3 years ago)
- Last Synced: 2025-03-25T22:32:13.722Z (about 1 year ago)
- Topics: flask, mongodb, mongomock, pytest, pytest-mock, python
- Language: Python
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Testing a pymongo/Flask app using pytest and mongomock
This repo contains an example on how to use mongomock to replace a `pymongo.MongoClient` inside a Flask app.
The dependencies, test cases, and structure of the app are based on a real project where I was scratching my head over this exact problem.
The setup of the `db.MongoDB` class imported and initialized in app.py is based on the MongoAdapter in [Clinical-Genomics/scout](https://github.com/Clinical-Genomics/scout/blob/main/scout/adapter/mongo/base.py)
To try it out, clone this repo, install the dependencies in `requirements.txt` and run pytest:
``` python
git clone https://github.com/alkc/flask-mongomock-example.git
cd flask-mongomock-example
pip install --requirement requirements.txt
pytest .
```
I'm open for feedback if you think there is a smarter/better way of doing this.