Django 禁用Migrations 给 Unit Test 提速

文章地址:https://simpleisbetterthancomplex.com/tips/2016/08/19/django-tip-12-disabling-migrations-to-speed-up-unit-tests.html

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值