Skip to main content

pytest plugin for MongoDB fixtures

Project description

https://img.shields.io/pypi/v/pytest-mongodb.svg https://travis-ci.org/mdomke/pytest-mongodb.svg?branch=master https://img.shields.io/pypi/l/pytest-mongodb.svg

What is this?

This is a pytest plugin, that enables you to test your code that relies on a database connection to a MongoDB and expects certain data to be present. It allows you to specify fixtures for database collections in JSON/BSON or YAML format. Under the hood we use the mongomock library, that you should consult for documentation on how to use MongoDB mock objects. If suitable you can also use a real MongoDB server.

Note: This project has been renamed from humongous to pytest-mongodb in order to conform to the pytest plugin naming conventions and to be more easy to find on the Python package index. See the migration section for more information.

Configuration

If you don’t want to put your fixtures on the top-level directory of your package you have to specify a directory where pytest-mongodb looks for your data definitions.

To do so put a line like the following under the pytest section of your pytest.ini-file put a

[pytest]
mongodb_fixture_dir =
  tests/unit/fixtures

pytest-mongodb would then look for files ending in .yaml or .json in that directory.

You can also choose to use a real MongoDB server for your tests. In that case you might also want to configure the hostname and/or the credentials if you don’t want to stick with the default (localhost and no credentials). Use the following configuration values in your pytest.ini to adapt the settings to your needs:

[pytest]
mongodb_engine = pymongo
mongodb_host = mongodb://user:passwd@server.tld
mongodb_dbname = mydbname

Basic usage

After you configured pytest-mongodb so that it can find your fixtures you’re ready to specify some data. Regardless of the markup language you choose, the data is provided as a list of documents (dicts). The collection that these documents are being inserted into is given by the filename of your fixture-file. E.g.: If you had a file named players.yaml with the following content:

-
  name: Mario
  surname: Götze
  position: striker

-
  name: Manuel
  surname: Neuer
  position: keeper

you’d end up with a collection players that has the above player definitions inserted. If your fixture file is in JSON/BSON format you can also use BSON specific types like $oid, $date, etc.

You get ahold of the database in you test-function by using the humongous fixture like so:

def test_players(mongodb):
    assert 'players' in mongodb.collection_names()
    manuel = mongodb.players.find_one({'name': 'Manuel'})
    assert manuel['surname'] == 'Neuer'

For further information refer to the mongomock documentation.

If you want to skip specific tests if the engine is ie. a mongomock engine you could to that like so:

from pytest_mongodb import mongo_engine
from pytest import mark

@mark.skipif(mongo_engine() == 'mongomock', reason="mongomock does not support that")
def test_players(mongodb):
    assert 'players' in mongodb.collection_names()
    manuel = mongodb.players.find_one({'name': 'Manuel'})
    assert manuel['surname'] == 'Neuer'

Migration from humongous

In the course of migrating the package name from humongous to pytest-mongodb most configuration values which previously were prefixed with humongous_ have been renamed to a mongodb_-prefixed counterpart. The only notable exception is the humongous_basedir config value, which now is named mongodb_fixture_dir. Additionally the commandline options have been unified, in a way that multi-word option names are now consistently separated with dashes instead of underscores.

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-mongodb-2.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

pytest_mongodb-2.1.0-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-mongodb-2.1.0.tar.gz.

File metadata

File hashes

Hashes for pytest-mongodb-2.1.0.tar.gz
Algorithm Hash digest
SHA256 5ce8d57d85f41beccc46ee3594e6df082f9679f157bba1f1111b679340a69b56
MD5 e432d9c83a2e9b214378c771762a82bb
BLAKE2b-256 0f826ca6e35b8163b4cbdbcd01ccf0bebb2955f58f9d089d72cf0cf269c837f8

See more details on using hashes here.

File details

Details for the file pytest_mongodb-2.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_mongodb-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb70483004c2cac7d16e03bec4710b5a18679c88a0b990c1be6508020092b050
MD5 14ded131f5bc983728ec61f559600f99
BLAKE2b-256 b357bf78f66843b0dd8a9372ba8fea3c1deb3ff249a4f9861566995b449e78fb

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