I am about to finish a university project that involves Python, and my team has a test case shortage. We need more, and specifically, we need some test cases for the database. This is where mocking comes in, however. We need a Python-specific library that can be used to mock a database, so the test cases do not use SQL or access the real database. We are using MySQL for the real database. It's a bit difficult for me to come across an easy-to-use Python database mocking library. What would be the best one to use for our test cases? Or, is there a better way to test a database in Python without touching it? I would appreciate your advice very much. Thanks :-)
-
Can you not just create a test version of the database? If for some reason you don't want to or can't use MySQL for testing, SQLite is bundled with Python as of version 2.7 – tMC May 10 '11 at 19:13
-
Does Python use any built-in database, like Ruby does? – Michael Minkov May 10 '11 at 19:30
-
I know ruby on rails the framework has an abstraction so you don't have to construct SQL, is that what you're referring to? Python the language doesn't have anything like that builtin; if you're talking about what I'm thinking, its a function of framework, not the ruby language. I could be completely wrong about this. – tMC May 10 '11 at 19:35
-
Yeah, I'm talking about the framework, Rails. We are using the Python framework Django. – Michael Minkov May 10 '11 at 19:56
-
1Unfortunately I think you're looking for functionality in the wrong place. Python the language doesn't offer what you are looking for, but I don't think any do. Going along with Eli, it sounds like you might need to configure a test instance of Django/MySQL – tMC May 10 '11 at 20:08
-
would just using SQLite do the trick? – Michael Minkov May 10 '11 at 20:59
-
With SQLite you'll still need to construct your SQL etc. It doesn't really offer any better abstraction than the MySQLdb module. I mentioned it just as a builtin alternative should you be trying to get away from MySQL. Here is example of how you can abstract some of the SQL- http://www.devshed.com/c/a/Python/Using-SQLite-in-Python/4/ however, you might end up spending more time trying to make this a seamless ingratiation than just creating a test env (if thats what you are after) – tMC May 10 '11 at 21:07
-
this is a project that simulates SQL in-process, in python. https://pypi.python.org/pypi/pg13. (fair warning: I wrote it) – amwinter Feb 03 '15 at 08:30
5 Answers
If you absolutely must use mocks rather than the Django stuff others have mentioned, I'd recommend either Fudge or Mock.

- 1,736
- 12
- 11
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/

- 54,664
- 18
- 108
- 145
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.

- 186,300
- 67
- 213
- 256
-
Thanks...but my leader requires a mocking library...does Python have a good, programmer-friendly one? – Michael Minkov May 10 '11 at 20:18
-
If your tests really work with a database, then they aren't "unit-tests" but they are "integration tests" – Dimitre Novatchev Apr 23 '15 at 22:54
Based on some comments, it appears you're using Django. If that's the case, then you're going to want to use a data fixture to populate test data into a test database. An excellent resource on this topic is Karen M. Tracey's book Django 1.1 Testing and Debugging.
Here's a summary of what you're going to want to do:
- Enter data into your live/real database using the admin interface.
Dump the data from your live database using:
python manage.py dumpdata {django-app} --indent 4 >test_data.json
replacing
{django-app}
with the name of your Django app.Place
test_data.json
in the directory{django-app}/fixtures
Specify that your test case should load the fixture data using:
Class MyTest(TestCase): fixtures = ['test_data.json'] def testThisCode(self): # Test code
For more information, in addition to Karen M. Tracey's book, you might check out Django's documentation on Fixture Loading

- 457,139
- 39
- 126
- 163