Skip to main content

MongoDB process and client fixtures plugin for Pytest.

Project description

https://raw.githubusercontent.com/ClearcodeHQ/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 enables you to test your code that relies on a running MongoDB database. It allows you to specify fixtures for MongoDB process and client.

How to use

Plugin contains two fixtures

  • mongodb - it’s a client fixture that has functional scope, and which cleans MongoDB at the end of each test.

  • mongo_proc - session scoped fixture, that starts MongoDB instance at the first use and stops at the end of the tests.

  • mongo_noproc - a no process fixture, that’s connecting to already running mongodb instance. For example on dockerized test environments, or CI providing mongodb services

Simply include one of these fixtures into your tests fixture list.

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

from pytest_mongo import factories

mongo_my_proc = factories.mongo_proc(
    port=None, logsdir='/tmp')
mongo_my = factories.mongodb('mongo_my_proc')

Connecting to already existing mongodb database

Some projects are using already running MongoDB servers (ie on docker instances). In order to connect to them, one would be using the mongo_noproc fixture.

mongo_external = factories.mongodb('mongo_noproc')

By default the mongo_noproc fixture would connect to MongoDB instance using 27017 port. Standard configuration options apply to it.

These are the configuration options that are working on all levels with the mongo_noproc fixture:

Configuration

You can define your settings in three ways, it’s 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

Noop process fixture

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

Path to store logs

logsdir

–mongo-logsdir

mongo_logsdir

no

$TMPDIR

Additional parameters

params

–mongo-params

mongo_params

no

MongoDB client’s time zone awarness

tz_aware

–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)
  • use --mongo-port command line option when you run your tests

    py.test tests --mongo-port=8888
  • specify your directory 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

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-3.0.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

pytest_mongo-3.0.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest-mongo-3.0.0.tar.gz.

File metadata

  • Download URL: pytest-mongo-3.0.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for pytest-mongo-3.0.0.tar.gz
Algorithm Hash digest
SHA256 f8618ace44ce50bf2f4f4f4d37e27f1a95c51a1089f8093577a88e4de82c92aa
MD5 4fcf6808ff0939a98d287d52f08a6cb4
BLAKE2b-256 2e9c6fe77649ff9fc16164c2baec038673f03b699212e2b1da15f9eea4508dc4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_mongo-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for pytest_mongo-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 235f9966e915874c98d61148a062b02bc41df8941c12e76ad4e6ab441c3149c6
MD5 90ff48d64ef450b361ac8c6f20e4142d
BLAKE2b-256 4f7e5e5bfce516fd887ab651291b5ac9717587d46d9a955ec5934e02b54d3059

See more details on using hashes here.

Supported by

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