在Django 1.7中运行单元测试时禁用迁移 [英] Disable migrations when running unit tests in Django 1.7

查看:99
本文介绍了在Django 1.7中运行单元测试时禁用迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.7 引入了数据库迁移

Django 1.7 introduced database migrations.

在Django 1.7中运行单元测试时,它会强制迁移,这需要很长时间。所以我想跳过django迁移,并在最后的状态创建数据库。

When running the unit tests in Django 1.7, it forces a migrate, that takes a long time. So I would like to skip the django migrations, and create the database in the final state.

我知道忽略迁移可能是一个不好的做法,因为这部分代码将不会被测试。但事实并非如此:我正在CI测试服务器(jenkins)中运行完整的迁移。我只想跳过本地测试中的迁移,速度很重要。

I know that ignoring the migrations can be a bad practice, as that part of the code would not be tested. But that's not the case: I'm running the full migrations in the CI test server (jenkins). I only want to skip the migrations in my local tests, where the speed matters.

某些上下文:

直到Django 1.6 ,当使用South时,我使用了 SOUTH_TESTS_MIGRATE 设置:

Until Django 1.6, when using South, I used the SOUTH_TESTS_MIGRATE setting:


默认情况下,South的syncdb命令如果运行非交互模式(包括运行测试时运行测试),它将会运行迁移,每次运行测试时都会运行迁移。

By default, South’s syncdb command will also apply migrations if it’s run in non-interactive mode, which includes when you’re running tests - it will run every migration every time you run your tests.

如果您希望测试运行程序使用syncdb而不是迁移 - 例如,如果您的迁移过长时间无法应用 - 只需在settings.py中设置SOUTH_TESTS_MIGRATE = False。

If you want the test runner to use syncdb instead of migrate - for example, if your migrations are taking way too long to apply - simply set SOUTH_TESTS_MIGRATE = False in settings.py.

但是,不再存在 syncdb ,现在它已经迁移。

However, syncdb does not exist anymore, now it's migrate.

而从Django 1.8 我会使用 - keepdb 参数:

And from Django 1.8 I'll use the --keepdb parameter:


--keepdb选项可用于在测试运行之间保留测试数据库。这具有跳过创建和销毁动作的优点,这大大减少了运行测试的时间,特别是大型测试套件中的测试。如果测试数据库不存在,它将在第一次运行时创建,然后为每个后续运行保留。任何未应用的迁移也将在运行测试套件之前应用于测试数据库。

The --keepdb option can be used to preserve the test database between test runs. This has the advantage of skipping both the create and destroy actions which greatly decreases the time to run tests, especially those in a large test suite. If the test database does not exist, it will be created on the first run and then preserved for each subsequent run. Any unapplied migrations will also be applied to the test database before running the test suite.

所以这个问题仅限于Django 1.7。 / p>

So this question is limited to Django 1.7.

推荐答案

查看此解决方法,由Bernie Sumption发布到Django开发人员邮件列表中:

Look at this workaround, posted by Bernie Sumption to the Django developers mailing list:


如果还没有运行makemigrations,migrate命令将
的应用程序视为未迁移,并直接从模型创建表,只需
,就像syncdb在1.6中所做的那样。我定义了一个新的设置模块,单元
测试称为settings_test.py,它从主
设置模块导入*,并添加以下行:

If makemigrations has not yet been run, the "migrate" command treats an app as unmigrated, and creates tables directly from the models just like syncdb did in 1.6. I defined a new settings module just for unit tests called "settings_test.py", which imports * from the main settings module and adds this line:

MIGRATION_MODULES = {myapp:myapp.migrations_not_used_in_tests}

MIGRATION_MODULES = {"myapp": "myapp.migrations_not_used_in_tests"}

然后我运行这样的测试:

Then I run tests like this:

DJANGO_SETTINGS_MODULE =myapp.settings_testpython manage.py test

DJANGO_SETTINGS_MODULE="myapp.settings_test" python manage.py test

这个傻瓜迁移到认为该应用没有移植,所以每次b $ b测试数据库被创建,它反映了models.py的当前
结构。

This fools migrate into thinking that the app is unmigrated, and so every time a test database is created it reflects the current structure of models.py.

在Django 1.9中,这种情况有所改进,您可以将值设置为

In Django 1.9, this situation is improved somewhat, and you can set the value to None:

MIGRATION_MODULES = {myapp:无}

MIGRATION_MODULES = {"myapp": None}

这篇关于在Django 1.7中运行单元测试时禁用迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆