Skip to main content

A Python library to ease testing applications that rely on MongoDB as a datastore

Project description

# MongoDB Test Util

This library provides a handful of useful functionality for easing the testing of systems that rely on MongoDB as a datastore

## Start and Teardown mongod

For most use cases, you can use this snippet to startup a mongod instance for your test environment

from mongo_test.handlers import startup_handle startup_handle()

To tear an instance down if one exists:

from mongo_test.handlers import teardown_handle teardown_handle()

## Fixtures

MongoTest allows you to specify fixtures in yaml, with a few conveniences


from mongo_test.fixtures import setup_data import_data([path, …])

A sample yaml file would look something like:

collection: users drop: true
_id: !oid 1 username: idbentley first_name: Ian last_name: Bentley

!oid is a custom constructor provided by MongoTest to allow you to test object ids as having any integer seed you specify. You can access the constructor directly with:

from mongo_test.fixtures import oid_con id = oid_con(1)

## A complete example

Assuming that you’ve used the startup_handler to setup a mongod and that we have the above user_fixture.yml on path.

import pymongo from mongo_test.fixtures import setup_data, oid_con

conn = MongoClient(port=27018) db = conn.myapp

def setup():
# Load fixture before each test. setup_data([‘path/to/user_fixture.yml’], db)
def test_find_user():
# Fetch user by id user = db.users.find_one(query={‘_id’: oid_con(1)}) # Check pymongo works as advertised. assert account[‘_id’] == oid_con(1) assert account[‘username’] == “idbentley”

Project details

Release history Release notifications

History Node


This version
History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
MongoTest-0.1.0.tar.gz (3.6 kB) Copy SHA256 hash SHA256 Source None Jan 10, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page