MongoDB Helpers

class test_helpers.mongo.TemporaryDatabase(**kwargs)

Creates a temporary MongoDB database that is destroyed automatically.

Parameters:
  • host (str) – Database to connect to. This defaults to :envvar: MONGOHOST or localhost if omitted.
  • port (int) – Port number that the database is listening on. This defaults to :envvar: MONGOPORT` or ``27017 if omitted.

Instances of this class will create a bare MongoDB database with a single collection named test_helpers containing a single document with a create date for tracking purposes. When the test process exits all databases created will be destroyed automatically. Under the hood it uses pymongo and registers a single cleanup function with :func`atexit.register`.

Usage Example

from test_helpers import mongo

_testing_db = mongo.TemporaryDatabase()

def setup_module():
    _testing_db.create()
create()

Create the temporary database if it does not exist.

drop()

Drop the temporary database if it was created.

set_environment()

Export MongoDB environment variables for the database.

This exports the MONGOHOST, MONGOPORT, and MONGODATABASE environment variables