使用AUTH_USER_MODEL测试Django 1.5

时间:2013-03-12 02:51:46

标签: django django-testing django-1.5

我将我的django应用程序迁移到Django 1.5。我配置了AUTH_USER_MODEL,一切看起来都不错,但是当我尝试创建一些测试时,我收到了以下错误:

DatabaseError: (1146, "Table 'test_X.auth_user' doesn't exist")

tests.py

class XXTest(unittest.TestCase):

    def setUp(self):
        self.data= {
            'password1':'aaaaa',
            'password2':'aaaaa',                
            'city':'NY', (....)

        }
        self.client = Client()

    def test_register(self):
        c = Client()
        resp = self.client.post('/register/user/', self.data)
        self.assertEqual(resp.status_code, 200)

Settings.py

    INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
  ...
    'myuser',
)
AUTH_USER_MODEL = 'myuser.MyUser'

我使用的是MySQL,实际上没有在测试数据库中创建表 myuser 。 如果我运行python manage.py migrate我没有错误。但如果我跑:

python manage.py syncdb

Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.messages
 > django.contrib.staticfiles
 > django.contrib.admin
 > django.contrib.flatpages
 > django.contrib.sitemaps

...     未同步(使用迁移): ......

- myuser
(use ./manage.py migrate to migrate these)

如何让我的测试正常运行?

1 个答案:

答案 0 :(得分:4)

您必须在myuser / models.py

上创建自定义用户

就像https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example

一样