skillfed

mongomock

Fake pymongo stub for testing simple MongoDB-dependent code

mongomock v4.3.0 7.9M downloads/30d#1,680 on PyPI1,003
Permissive license ISC License Copyright (c) 2024, Monogomock Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above… (full text in the JSON record) AGING released

What it is and what it does

Mongomock is a testing library that replaces pymongo's MongoClient with an in-memory mock, allowing you to test MongoDB-dependent code without setting up or managing a real MongoDB instance. It implements enough of the MongoDB API to behave like a real collection for typical query and update operations, making tests faster and more portable across CI environments and local machines.

The library is designed for functional testing rather than as a perfect replica of MongoDB. When a feature is not yet implemented, it raises NotImplementedError rather than silently behaving differently from production pymongo, ensuring your tests fail where production code would fail. It supports both direct instantiation (creating a mock client in your test) and decorator-based patching (intercepting pymongo.MongoClient calls in code under test).

Use it for:

  • Unit test code that queries and updates MongoDB collections without spinning up a test database.
  • Run test suites in CI/CD pipelines where MongoDB infrastructure is unavailable or undesirable.
  • Validate application logic against MongoDB operations in isolation, comparing expected vs. actual collection state.
  • Migrate code from pymongo v3 to v4 by running tests against both versions to catch API incompatibilities early.
  • Mock MongoDB interactions in decorator-patched functions that create their own pymongo connections.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Mongomock provides an in-memory mock of MongoDB collections for testing Python code that uses pymongo, without requiring a running MongoDB instance.

Yes. Mongomock is production-stable, widely used, has no known vulnerabilities, and solves a real testing problem with low install friction. The aging maintenance status is a minor concern but the repository remains active and the ISC license is permissive. Install it if you test pymongo-dependent code.

Install

mongomock on PyPI

pip

pip install mongomock

uv

uv add mongomock

poetry

poetry add mongomock

Installing mongomock

Before you install

Low install friction: pure Python wheel with only three runtime dependencies (packaging, pytz, sentinels). Last release was 636 days ago; repository is active and not archived, though maintenance status is aging.

License in practice

ISC License (permissive): you may use, modify, and distribute mongomock freely in commercial and private projects with minimal restrictions—only requiring preservation of copyright and license notices.

Quickstart

import mongomock

client = mongomock.MongoClient()
db = client.test_db
collection = db.test_collection

collection.insert_one({'name': 'test', 'value': 1})
result = collection.find_one({'name': 'test'})
print(result)

Verify before relying

  • Completeness of MongoDB API coverage relative to pymongo v4—the description notes TDD-driven feature addition but does not enumerate which operations are supported.
  • Whether the package works with pymongo v5 or later versions beyond v4.
  • Monthly download volume and ranking position reflect current popularity but may not indicate active maintenance frequency.

Package facts

License ISC License Copyright (c) 2024, Monogomock Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above… (full text in the JSON record) (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies 3 — packaging, pytz, sentinels
Maintenance aging — 636 days since the last release
Last repo commit
First released
Downloads 7,923,784/month — #1,680 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: mongomock-4.3.0-py2.py3-none-any.whl

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: ISC License (ISCL)Operating System :: MacOS :: MacOS XOperating System :: Microsoft :: WindowsOperating System :: POSIXProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Database

Tags

mongodb mock testingpymongo test doublein-memory mongodbmongodb fake for testspymongo unit testingmongodb collection mocktest mongodb without server
testingmongodbmock

More Database packages