且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django测试使用现有数据库

更新时间:2023-01-30 23:07:45

快进到2016年,在django内部已经建立了在测试之间保留数据库的能力。它的形式可以是 --keep标记 to manage.py

Fast forward to 2016 and the ability to retain the database between tests has been built into django. It's available in the form of the --keep flag to manage.py


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

New in Django 1.8. Preserves the test database between test runs. This has the advantage of skipping both the create and destroy actions which can greatly decrease 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.

这个漂亮完全填写您在问题中提到的所有标准。事实上,它甚至进一步。在每次运行之前都不需要导入转储。

This pretty much fullfills all the criteria you have mentioned in your questions. In fact it even goes one step further. There is no need to import the dump before each and every run.