Django 框架提供了 test 接口,很简单,自从1.7版本之后添加了 migrations 特性,默认的 test 命令会在每次执行的时候进行 migrations, 导致单元测试很慢啊。。咋办,禁用啊。
一般情况下都会为 test 单独设置一个 settings 配置
比如 tests_settings.py
from settings import *
# Custom settings goes here
然后这样执行测试
python manage.py test --settings=myproject.tests_settings --verbosity=1
Django >=1.8 参数方式
使用keepdb 选项 the test databases
When using the SQLite database engine the tests will by default use an in-memory database
python manage.py test --keepdb
Django >= 1.9
1.9 中有这么一个配置项目 MIGRATION_MODULES
。文档
from settings import *
MIGRATION_MODULES = {
'auth': None,
'contenttypes': None,
'default': None,
'sessions': None,
'core': None,
'profiles': None,
'snippets': None,
'scaffold_templates': None,
}
Django < 1.9
from settings import *
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return 'notmigrations'
MIGRATION_MODULES = DisableMigrations()
Older Django Versions (using South)
SOUTH_TESTS_MIGRATE = False