简体   繁体   中英

Creating a Testing Database Django

I have a mysql database defined in settings.py

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME':'<name>',
            'USER':'<user>',
            'PASSWORD':'<password>'
        }
    }

Now I need to create a test database for my unittesting. Where do I need to mention the settings of test database or I don't have to mention them at all?

For testing when I do:

python manage.py test my_app

it says:

Creating test database for alias 'default'...
Skipping creation of NoticeTypes as notification app not found
E
======================================================================
ERROR: test_update_quiz (toolbox.tests.TestQuizCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/2.0/site/toolbox/tests.py", line 20, in test_update_quiz
    akit = AssignmentKit.objects.get(pk = akit_id)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 131, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 366, in get
    % self.model._meta.object_name)
DoesNotExist: AssignmentKit matching query does not exist.

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (errors=1)
Destroying test database for alias 'default'...

The error the test gives is wrong. Because I do have that object in my database I checked from ORM. Why is this error coming? are my database not properly linked?

The docs for testing do explain this. You don't create a database for tests, Django does it automatically when the tests are run. But the db is created from scratch each time, so your tests themselves need to populate the db with any required data: either via fixtures, or by creating the entities directly in the test or in the setUp method.

The whole point of unit tests is that they are entirely self-contained, and don't rely on any external state: using an existing database would violate that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM