Django Unitest: Table doesn't exist
<p>I created a simple test case like this:</p>

<pre><code>from unittest import TestCase
import user_manager


class UserTest(TestCase):
    def test_register(self):
        email = "dung7@gmail.com"
        password = "123456"
        result, user = user_manager.setup_new_user(email, password)
        self.assertEqual(result, CodeID.SUCCESS)
</code></pre>

<p>Then I run the testcase:</p>

<pre><code>python manage.py test users
</code></pre>

<p>And here is the log:</p>

<pre><code>Creating test database for alias 'default'...
/Users/admin/Projects/MyApp/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py:112: Warning: Table 'mysql.column_stats' doesn't exist
  return self.cursor.execute(query, args)
Creating test database for alias 'myapp_log'...
.FE
======================================================================
ERROR: test_register (users.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
...
ProgrammingError: (1146, "Table 'test_myapp.user' doesn't exist")
</code></pre>

<p>So it created a test database but seem like it didn't create the tables. Here is my DATABASES setting:</p>

<pre><code>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "myapp",
        'USER': "root",
        'PASSWORD': "",
        'HOST': '127.0.0.1',
        'PORT': '',
    },
    'myapp_log': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "myapp_log",
        'USER': "root",
        'PASSWORD': "",
        'HOST': '127.0.0.1',
        'PORT': '',
    },
}
</code></pre>

<p>And my model:</p>

<pre><code>class User(BaseModel):
    uid = models.IntegerField(primary_key=True)
    email = models.CharField(max_length=200)
    password = models.CharField(max_length=200)
    create_time = models.IntegerField()
    update_time = models.IntegerField()
    status = models.IntegerField()
    social_token = models.CharField(max_length=200)
    social_app = models.IntegerField()

    class Meta:
        db_table = 'user'
</code></pre>

<p>Anyone know why the table 'user' is not created?</p>

<p>UPDATE:</p>

<p>user_manager from my testcase will do some query and add new record on table user. </p>

<p>And I thought when I run the testcase, Django will somehow read my models and create a test database with all the table from those models. Am I right about this?</p>

<p>So I found out I need to put my models.py in my users folder, and add 'users' into the INSTALL_APPS setting. Now it worked.</p>

python mysql django django-unittest