Skip to main content

MongoDB process and client fixtures plugin for Pytest.

Project description

https://raw.githubusercontent.com/dbfixtures/pytest-mongo/master/logo.png

pytest-mongo

Latest PyPI version Wheel Status Supported Python Versions License

What is this?

This is a pytest plugin that helps you test code that relies on a running MongoDB database. It provides fixtures for a MongoDB process and client.

Project Architecture Diagram

How to use

Runtime requirements are defined in pyproject.toml.

The plugin contains three fixtures:

  • mongodb - a function-scoped client fixture that cleans MongoDB at the end of each test.

  • mongo_proc - a session-scoped fixture that starts a MongoDB instance on first use and stops it at the end of the test session.

  • mongo_noproc - a no-process fixture that connects to an already running MongoDB instance. For example, on dockerized test environments or CI providing MongoDB services.

Simply include one of these fixtures in your test or fixture list.

You can also create additional MongoDB client and process fixtures if you need to:

from pytest_mongo import factories

mongo_my_proc = factories.mongo_proc(port=None)
mongo_my = factories.mongodb('mongo_my_proc')

Connecting to an existing MongoDB database

Some projects use already running MongoDB servers (e.g., on docker instances). To connect to them, use the mongo_noproc fixture. Authentication/URI options are not yet supported (see issue #747).

mongo_external = factories.mongodb('mongo_noproc')

By default, the mongo_noproc fixture connects to a MongoDB instance on port 27017. Standard configuration options apply to it.

The following configuration options apply to the mongo_noproc fixture as well:

Configuration

You can define settings in three ways: fixture factory argument, command line option, and pytest.ini configuration option. You can pick which you prefer, but remember that these settings are handled in the following order:

  • Fixture factory argument

  • Command line option

  • Configuration option in your pytest.ini file

Configuration options

MongoDB server option

Fixture factory argument

Command line option

pytest.ini option

Applies to mongo_noproc

Default

Path to mongodb exec

executable

–mongo-exec

mongo_exec

no

/usr/bin/mongod

MongoDB host

host

–mongo-host

mongo_host

127.0.0.1

127.0.0.1

MongoDB port

port

–mongo-port

port

27017

random

Port search count

–mongo-port-search-count

mongo_port_search_count

5

Additional parameters

params

–mongo-params

mongo_params

no

MongoDB client’s time zone awareness (override with –mongo-tz-aware/–no-mongo-tz-aware)

tz_aware

–mongo-tz-aware, –no-mongo-tz-aware

mongo_tz_aware

no

False

Example usage:

  • pass it as an argument in your own fixture

    mongo_proc = factories.mongo_proc(port=8888)
  • pass additional mongod parameters via a single string

    mongo_proc = factories.mongo_proc(
        params="--quiet --setParameter diagnosticDataCollectionEnabled=false"
    )
  • use --mongo-port command line option when you run your tests

    pytest tests --mongo-port=8888
  • specify your port as mongo_port in your pytest.ini file.

    To do so, put a line like the following under the [pytest] section of your pytest.ini:

    [pytest]
    mongo_port = 8888

Compatibility (tested)

CI covers MongoDB 7.0 and 8.0; other versions may work but aren’t tested.

Package resources

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-4.0.0.tar.gz (18.9 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-4.0.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_mongo-4.0.0.tar.gz.

File metadata

  • Download URL: pytest_mongo-4.0.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_mongo-4.0.0.tar.gz
Algorithm Hash digest
SHA256 6560c12de486a9231d69e0c47285bc1898189c0974911cf8a775661617ebaac0
MD5 1b8e2d6d2e90f67d6b7cbff996daa6d0
BLAKE2b-256 ed8be7ed77e065ae415c54036c40acadf75f672ba0e7790db630a7bf48a8a280

See more details on using hashes here.

File details

Details for the file pytest_mongo-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_mongo-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_mongo-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07d47cecf04f0ac35dc069594344ca54b11d76ff0d02befe51e431c8f5c9ea4a
MD5 aa67f7940c37ba8723d74a7e2a1d2e88
BLAKE2b-256 7db4670fa998f4c9a1b7c79085ac0744c2570e8d8c80ae9837d65838e1ce415b

See more details on using hashes here.

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