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

查看:30
本文介绍了在 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 而不是 migrate - 例如,如果您的迁移花费的时间太长而无法应用 - 只需在 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 已经不存在了,现在它是 migrate.

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.

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,迁移"命令将处理未迁移的应用程序,并直接从模型创建表就像 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_test" python manage.py test

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

这个傻瓜迁移到认为应用程序未迁移,所以每次创建测试数据库时,它都会反映当前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 中,这种情况有所改善,您可以将值设置为 None:

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

MIGRATION_MODULES = {"myapp": None}

MIGRATION_MODULES = {"myapp": None}

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

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