2👍
In your settings.py
you can tell Django what database to use when testing. Here is the code to do so if you would like to use SQLite
for example:
settings.py:
if 'test' in sys.argv:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'tests.db',
}
Django
creates a new database every time you run your manage.py test
, so you would want to use fixtures in your test.py
file to preload you database with data before running tests (if you need it preloaded with data).
To generate a fixture for the User model
for example, use this code:
python manage.py dumpdata auth.User --indent=2 > app_name/fixtures/admin.json
To use fixtures
you would structure your tests.py
like so:
tests.py:
from django.test import TestCase
class MyTests(TestCase):
fixtures = [admin.json]
def setUp(self):
# other setup code here
def test_the_obvious(self):
assert True == True
Source:stackexchange.com