问题:
问题是我有一个可以正常工作的应用程序,但是当我尝试使用以下命令运行测试时:
coverage run manage.py test --settings=crm.settings.test
一开始就发生错误:
Creating test database for alias 'default'...
IntegrityError: profiles_usermodel.current_project_id may not be NULL
以前我进行了测试,一切都运行良好。然后我显着改变了模型和应用程序并尝试运行测试——结果我遇到了上面提到的问题。我做错了什么?
我的设置:
我有单独的测试设置:
My_application
|___My_application
|___urls.py
|___ __init__.py
|___wsgi.py
|___settings
|___base.py
|___test.py
|___local.py
|___profiles
|___models.py
|___views.py
|___tests
|___ __init__.py
|___models.py
测试设置如下:
"""Local test settings and globals which allows us to run our
test suite locally."""
from .base import *
########## TEST SETTINGS
TEST_RUNNER = 'discover_runner.DiscoverRunner'
TEST_DISCOVER_TOP_LEVEL = PROJECT_ROOT
TEST_DISCOVER_ROOT = PROJECT_ROOT
TEST_DISCOVER_PATTERN = "*"
########## IN-MEMORY TEST DATABASE
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "",
},
}
INSTALLED_APPS += ('coverage',)
还有来自My_application/profiles/models.py
(省略了几个领域和方法)的模型:
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser, PermissionsMixin )
from django.contrib.auth.models import Group
class Project(models.Model):
name = models.CharField(max_length=255)
class UserModel(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255, unique=True, db_index=True,)
current_project = models.ForeignKey(Group, related_name='usermodel_current')
USERNAME_FIELD = 'email'
我还使用 South 进行数据库迁移和 django 1.5c2。