Finding a Python Library to Mock a Database [closed]
If you are using django, fixtures can solve your problem. I don't really know what you mean by mocking library, but they allow you to fill your db with test data. Which you can then interact with for your TestCase. The test data is destroyed after each TestCase.http://docs.djangoproject.com/en/dev/howto/initial-data/
Personally I tend to write my unit tests to use a separate test database, so I'll usually have a testing.conf
file containing my database info separate from my regular development.conf
and/or production.conf
files. This makes my unit tests far more similar to the actual conditions under which the code will be executed in production.
For example, I recently developed a Python library for which the user calls a module-level initialize
function, passing in the name(s) of the config file(s). The initialize
function then uses the ConfigParser module to parse the config file(s), starts whatever threads it needs to run, creates urllib handlers, establishes database connections, etc.
This kind of setup allows for easy unit testing, since you can simply have your unit tests call into your initialize
function before executing, passing in a config that points to your test database.