Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use mock MongoDB server for unit test

I have to implement nosetests for Python code using a MongoDB store. Is there any python library which permits me initializing a mock in-memory MongoDB server?

I am using continuous integration. So, I want my tests to be independent of any MongoDB running server. Is there a way to mock mongoDM Server in memory to test the code independently of connecting to a Mongo server?

Thanks in advance!

like image 499
Joel James Avatar asked Apr 10 '13 00:04

Joel James


People also ask

Should I mock DB in unit tests?

Mocking and stubbing are the cornerstones of having quick and simple unit tests. Mocks are useful if you have a dependency on an external system, file reading takes too long, the database connection is unreliable, or if you don't want to send an email after every test.

What is Mongomock?

mongomock is a package to do just what the name implies, mocking a mongo database. To use with mongoengine, simply specify mongomock when connecting with mongoengine: connect('mongoenginetest', host='mongomock://localhost') conn = get_connection()

Can unit tests use databases?

Properties of unit database testingUnit tests can be automated, and you can script a set of database operations with the same ease as executing code; Unit tests are great for testing individual triggers, views, and sprocs.


1 Answers

You could try: https://github.com/vmalloc/mongomock, which aims to be a small library for mocking pymongo collection objects for testing purposes.

However, I'm not sure that the cost of just running mongodb would be prohibitive compared to ensuring some mocking library is feature complete.

like image 81
Ross Avatar answered Oct 06 '22 15:10

Ross