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')
Each MongoDB process fixture can be configured in a different way than the others through the fixture factory arguments.
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_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
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
|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|
|Path to store logs||logsdir||--mongo-logsdir||mongo_logsdir||no||$TMPDIR|
|MongoDB client's time zone awarness||tz_aware||--mongo-tz-aware||mongo_tz_aware||no||False|
pass it as an argument in your own fixture
mongo_proc = factories.mongo_proc(port=8888)
--mongo-portcommand line option when you run your tests
py.test tests --mongo-port=8888
specify your directory as
To do so, put a line like the following under the
[pytest]section of your
[pytest] mongo_port = 8888
- Bug tracker: https://github.com/ClearcodeHQ/pytest-mongo/issues