6👍
5👍
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/
- Django – http code 304, how to workaround in the testserver?
- Pylint (pylint_django) does not work when –django-settings-module flag is specified
- TypeError at /en/ Object of type '__proxy__' is not JSON serializable
- Django 1.6: How to access static files in view
- How to remove these '"' in django template
2👍
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.
- Apache mod_wsgi error: Forbidden You don't have permission to access / on this server
- Django mysqlclient install
- Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or directory
- 'TemplateDoesNotExist' error with creating Sitemap for Django app
1👍
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
- Performance, load and stress testing in Django
- Attaching pdf's to emails in django
- Django fixtures DateTimeField runtimeWarning
- How to paginate "Change List" page in Django admin?
- Django admin, extending admin with custom views
0👍
There are some specific examples on using Mock for database testing here:
- Proper declaration of an empty Django PostgreSQL JSONField default value in migration file
- Django template object type
- How can I hide a django label in a custom django form?
- PIL – libjpeg.so.8: cannot open shared object file: No such file or directory
- Django migration relation does not exist