pytest-mongo

raw JSON →
4.0.0 verified Mon Apr 27 auth: no python

MongoDB process and client fixtures plugin for Pytest. Provides fixtures to start a MongoDB process and provide a pymongo client. Version 4.0.0 requires Python >=3.10 and uses the pytest-dbfixtures framework for managing MongoDB processes.

pip install pytest-mongo
error ModuleNotFoundError: No module named 'pytest_mongo'
cause pytest-mongo installed but not imported correctly or missing in test environment.
fix
Install with 'pip install pytest-mongo' and verify by 'python -c "import pytest_mongo"'.
error pytest fixture 'mongo_client' not found
cause pytest_plugins not set or pytest-mongo plugin not loaded.
fix
Add 'pytest_plugins = ["pytest_mongo"]' to conftest.py or test file.
error pytest_mongo.executor.MongoExecutorException: Process failed to start: 'mongod' not found
cause MongoDB binary not found; plugin tries to download but may fail.
fix
Set MONGODB_BINARY env var to full path of mongod, e.g., 'mongod' if in PATH, or install MongoDB.
breaking pytest-mongo 4.0.0 drops support for Python <3.10 and may change fixture scopes. Ensure Python >=3.10 and review fixture scope parameters.
fix Upgrade Python to 3.10+ and adjust conftest.py fixture scopes if relying on session-scoped defaults.
gotcha The mongo_proc fixture starts a real MongoDB process. It downloads mongod if not found, which can be slow and may fail on restricted CI environments.
fix Set the MONGODB_BINARY environment variable to an existing mongod binary to skip download. Or use a pre-installed MongoDB in CI.
deprecated Direct use of 'mongo_process' or 'mongod' fixtures from old pytest-mongo-dbfixtures is deprecated. Use mongo_proc and mongo_client from pytest_mongo.
fix Replace pytest-mongo-dbfixtures with pytest-mongo and use mongo_proc and mongo_client.

Add pytest_plugins to conftest or test file, then use mongo_client fixture directly.

import pytest

pytest_plugins = ["pytest_mongo"]

def test_mongo(mongo_client):
    db = mongo_client.test_database
    collection = db.test_collection
    collection.insert_one({"name": "test"})
    assert collection.find_one({"name": "test"}) is not None