Mongo

In ordrer to enable mongo support you have to add testsuite.database.mongo.pytest_plugin to pytest_plugins list in your conftest.py file and configure mongodb collections.

By default testsuite starts mongodb sharded cluster. In this case mongodb installation is required.

Testsuite may start mongodb with custom ports, if following environment variables are specified:

  • TESTSUITE_MONGO_CONFIG_SERVER_PORT

  • TESTSUITE_MONGOS_PORT

  • TESTSUITE_MONGO_SHARD_PORT

You can force it to use your own mongo installation with command-line option --mongo=mongodb://host:port/.

Mongodb plugins re-fills the whole database with data on each test. It looks for data fixture static files using db_collection_name.json pattern. If no file is found collection is empty.

Currently mongo plugin uses synchronous pymongo driver.

Example integration:

pytest_plugins = [
    'testsuite.pytest_plugin',
    'testsuite.databases.mongo.pytest_plugin',
]

MONGO_COLLECTIONS = {
    'example_collection': {
        'settings': {
            'collection': 'example_collection',
            'connection': 'example',
            'database': 'example_db',
        },
        'indexes': [],
    },
}

@pytest.fixture(scope='session')
def mongodb_settings():
    return MONGO_COLLECTIONS

Environment variables

TESTSUITE_MONGO_CONFIG_SERVER_PORT

Use to override mongo config server port. Default is 27118.

TESTSUITE_MONGOS_PORT

Use to override mongos port. Default is 27217.

TESTSUITE_MONGO_SHARD_PORT

Use to override mongo shard port. Default is 27119.

TESTSUITE_MONGO_RS_INSTANCE_COUNT

Replica set instances count, one instance is started by default.

Fixtures

mongodb

testsuite.databases.mongo.pytest_plugin.mongodb()[source]

Returns CollectionWrapper instance. Collection can be accessed by name, e.g.:

def test_foo(mongodb):
    doc = mongodb.collection_name.find_one({'_id': 123})
    assert doc == {...}

pymongo.collection.Collection instance is returned.

mongo_connection_info

testsuite.databases.mongo.pytest_plugin.mongo_connection_info()[source]

Returns an instance of class testsuite.databases.mongo.connection.ConnectionInfo

Marks

pytest.mark.filldb

Specify custom per-test collection data file prefix.

pytest.mark.filldb(**suffixes)
Parameters:

suffixes – collection suffixes map, e.g. {'collection': 'suffix', ...}

In the following example plugin will look for filename db_collection_name_foo.json:

@pytest.mark.filldb(collection_name='foo')
def test_foo(...):

pytest.mark.nofilldb

Disables database loading.

pytest.mark.nofilldb()

Classes

class testsuite.databases.mongo.connection.ConnectionInfo[source]

Mongodb connection uri parameters

get_uri(dbname: Optional[str] = None, retry_writes: Optional[bool] = None) str[source]

Get mongodb connection uri