Skip to main content

pytest-mongo-docker

GitHub Actions Workflow Status codecov PyPI - Version PyPI Downloads PyPI - Python Version GitHub License

A pytest plugin that provides session-scoped MongoDB fixtures backed by Docker containers.

Features

  • Zero configuration — Fixtures work out of the box, no setup required
  • Automatic lifecycle management — Pulls images, allocates ports, starts containers, and cleans up after tests
  • Fast — Data directory mounted to tmpfs for maximum speed
  • Version-specific fixtures — Test against MongoDB 5, 6, 7, 8, or latest
  • Replica set fixtures — Drop-in variants with --replSet enabled for change stream support
  • Session-scoped — One container per test session, shared across all tests

Installation

pip install pytest-mongo-docker

Requirements

  • Python 3.10+
  • pytest 8.0+
  • docker 7.0+
  • Docker daemon running locally or accessible via DOCKER_HOST environment variable

Usage

Basic Example

The plugin automatically registers fixtures when installed. Import the Mongo type for type hints:

import pymongo
import pytest_mongo_docker


def test_pymongo(mongo: pytest_mongo_docker.Mongo):
    client = pymongo.MongoClient(host=mongo.host, port=mongo.port)

    db = client["test_db"]
    collection = db["test_collection"]

    collection.insert_one({"key": "value"})
    doc = collection.find_one({"key": "value"})

    assert doc["key"] == "value"

    client.close()

Available Fixtures

All fixtures are session-scoped and return a Mongo object with host and port attributes:

Standalone (default):

  • mongo — Latest MongoDB version (mongo:latest)
  • mongo_5 — MongoDB 5.x (mongo:5)
  • mongo_6 — MongoDB 6.x (mongo:6)
  • mongo_7 — MongoDB 7.x (mongo:7)
  • mongo_8 — MongoDB 8.x (mongo:8)

Replica set (required for change streams and transactions):

  • mongo_rs — Latest MongoDB version (mongo:latest)
  • mongo_5_rs — MongoDB 5.x (mongo:5)
  • mongo_6_rs — MongoDB 6.x (mongo:6)
  • mongo_7_rs — MongoDB 7.x (mongo:7)
  • mongo_8_rs — MongoDB 8.x (mongo:8)

Replica set fixtures require pymongo (pip install pymongo) and start MongoDB with --replSet rs0. When connecting, use directConnection=True to avoid topology-discovery issues from the Docker port mapping:

client = pymongo.MongoClient(
    f"mongodb://{mongo_6_rs.host}:{mongo_6_rs.port}/",
    directConnection=True,
)

Advanced Example: Configuring Environment Variables

Use session-scoped autouse fixtures to configure your application before tests run:

import os
import pytest
import pytest_mongo_docker


@pytest.fixture(scope="session", autouse=True)
def init_env(mongo_6: pytest_mongo_docker.Mongo) -> None:
    os.environ["MONGODB_CONNECTION_STRING"] = f"{mongo_6.host}:{mongo_6.port}"
    os.environ["MONGODB_DBNAME"] = "myapp"


def test_app():
    # Your application reads from environment variables,
    # no need to reference the fixture directly
    pass

Using with Motor (async)

import motor.motor_asyncio
import pytest
import pytest_mongo_docker


@pytest.mark.asyncio
async def test_motor(mongo: pytest_mongo_docker.Mongo):
    client = motor.motor_asyncio.AsyncIOMotorClient(host=mongo.host, port=mongo.port)

    db = client["test_db"]
    collection = db["test_collection"]

    await collection.insert_one({"key": "value"})
    doc = await collection.find_one({"key": "value"})

    assert doc["key"] == "value"

    client.close()

How It Works

The plugin uses Docker's Python API to:

  1. Pull the specified MongoDB image (if not already cached)
  2. Allocate an unused local port
  3. Create a container with data directory mounted to tmpfs
  4. Start the container and wait for MongoDB to be ready
  5. Yield the Mongo object with connection details
  6. Kill and remove the container after all tests complete

Release files for pytest-mongo-docker 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-mongo-docker 0.1.2
File Size Uploaded
pytest_mongo_docker-0.1.2.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-mongo-docker 0.1.2
File Interpreter ABI Platform
pytest_mongo_docker-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / pytest_mongo_docker-0.1.2.tar.gz

Download URL pytest_mongo_docker-0.1.2.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
17fea9999789c730074ddd18c51420ff2291b287d0f82de2d6276e57052b205b
BLAKE2b-256 checksum
How to use checksums
44430ef4e8e32a47088095334369e14e6a7d66341f65fa377624203ee6fdcfba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / pytest_mongo_docker-0.1.2-py3-none-any.whl

Download URL pytest_mongo_docker-0.1.2-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4404583a8efe4474229c4d203330d92486dc7dbb620a1f566dca5ae3caebe4ff
BLAKE2b-256 checksum
How to use checksums
bac0c0d3d4db71ca7db15f6dbbf1e8d61f5d09e0d997ddedba28b219628b2147
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page