Correction: running Django tests with MongoDB is NOT slow

30 May 2010   1 comment   Django, MongoDB

Powered by Fusion×

At Euro DjangoCon I met lots of people and talked a lot about MongoDB as the backend. I even did a presentation on the subject which led to a lot of people asking me more questions about MongoDB.

I did mention to some people that one of the drawbacks of using MongoDB which doesn't have transactions is that you have to create and destroy the collections (like SQL tables) each time for every single test runs. I thought this was slow. It's not

Today I've been doing some more profiling and testing and debugging and I can conclude that it's not a problem. Creating the database has a slight delay but it's something you only have to do once and actually it's very fast. Here's how I tear down the collections in between each test:

class BaseTest(TestCase):

   def tearDown(self):
       for name in self.database.collection_names():
           if name not in ('system.indexes',):

For example, running test of one of my apps looks like this:

$ ./ test myapp
Ran 55 tests in 3.024s

So, don't fear writing lots of individual unit tests. MongoDB will not slow you down.


Philip Gatt
I was dropping mongo databases and it was WAY too slow. Thanks for your post. This approach is much faster.
Thank you for posting a comment

Your email will never ever be published

Related posts

Muted conversations in Gmail 29 May 2010
TfL Traffic cameras on a Google map 16 June 2010
Fastest database for Tornado 09 October 2013
Persistent caching with fire-and-forget updates 14 December 2011
Optimization story involving something silly I call "dict+" 13 June 2011
Mocking DBRefs in Mongoose and nodeunit 14 April 2011
EditDistanceMatcher - NodeJS script for doing edit distance 1 matching 05 February 2011
How I made my MongoDB based web app 10 times faster 21 October 2010
mongoengine vs. django-mongokit 24 May 2010
Speed test between django_mongokit and postgresql_psycopg2 09 March 2010 - Using (py)inotify to run commands when files change 20 July 2009
setuptools usability - not good, what can be done? 15 July 2009