In my previous post about unit testing for django, I laid the groundwork for how to unit test any django application. One nice feature that django includes with its test framework is the test database syncing. Even better is if you are using South to do database migrations - it will run the migrations in your test environment for you.
However, what if you are using a NoSQL database backend like MongoDB, Cassandra, CouchDB or something similar and you aren’t using the Django ORM? How do you handle setting up and tearing down the database environments?
The good news is that Python’s unittest framework makes this easy. You can override the
tearDown() on each TestCase that you build. Here is a snippet to get you started:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
What this does is
setUp() a collection in the
mytestcollection collection, runs the
my_doc_published test and then tears down the test database environment by dropping the
Things to remember for
setUp()is called before every test method in your TestCase class.
tearDown()is called after every test method in your TestCase class.
tearDown()is called even if your test methods fail or error out.
And there you have it! Django makes testing even non-ORM datasources a snap, if you know how to wire it up. UPDATE: Some would say that database fixtures and setting up/tearing down database environments as part of your unit tests is not “unit testing”. This is not entirely accurate, because in order to do unit tests that rely on backend data, you must instantiate and tear down pristine database environments.