Skip to main content

A tiny plugin for pytest which runs MongoDB in Docker

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_mongo_docker-0.1.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_mongo_docker-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_mongo_docker-0.1.1.tar.gz.

File metadata

  • Download URL: pytest_mongo_docker-0.1.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pytest_mongo_docker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5604a00a2681e37362adfe8e535c9863f45167ccfc14a0eae61017c6630088c2
MD5 94c11fcee23675a1488db58890c6ee34
BLAKE2b-256 0567641598f6550839ebae88473031015af025b17a2c3f161c5f7bd644a44f99

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_mongo_docker-0.1.1.tar.gz:

Publisher: publish.yml on anna-money/pytest-mongo-docker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_mongo_docker-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_mongo_docker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b6522da98d7c48a55f2fee32ad6f016ebcbce112b34e07317f813fd1cb8af73
MD5 b3166dce3b1a805dc5b20c55616f2316
BLAKE2b-256 72078209e7fa1f95bc4abd1e1ca9b5eddedae8e52de825f2b45ddac00ebb65ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_mongo_docker-0.1.1-py3-none-any.whl:

Publisher: publish.yml on anna-money/pytest-mongo-docker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page