0

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?

1

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I get it. My test_database doesn't have the objects I'm testing on. So in setUp method I have to import them from production db. Now how do I do that?
You don't import them from production db. You create the state you want to test "by hand".
My tests are bit complicated. So in setUp method I'm importing everything to the test database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.