Creating test cases for MongoDB transactions
The formulation of the test files, classes, and functions is the same when testing components with the Flask application running on MongoDB. The only difference is how pytest
will mock the MongoDB connection to pursue testing.
This chapter showcases the mongomock
module and its MongoClient
mock object that can replace a configured MongoDB connection. So, install the mongomock
module using the following pip
command before creating the test file:
pip install mongomock
Chapter 7 has a Tutor Finder application with components running on NoSQL databases such as MongoDB. The application uses the connect()
method of the mongoengine
module to establish a MongoDB connection for a few of the APIs. Instead of using the configured connection in the Flask’s app
context, a MongoClient
object from mongomock
can replace the mongoengine
’s connect()
method with a fake one. The following snippet of the test_tutor_login.py
file mocks the...