Django测试使用现有数据库 [英] Django test to use existing database

查看:732
本文介绍了Django测试使用现有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难定制测试数据库设置行为。我想实现以下内容:

I'm having a hard time customizing the test database setup behavior. I would like to achieve the following:


  • 测试套件需要使用现有的数据库

  • 测试套件不应该删除或重新创建数据库,而是从mysql转储中加载数据

  • 由于db从一个转储中填充,所以不应该加载灯具

  • 完成测试后,不应该销毁数据库

  • The test suites need to use an existing database
  • The test suite shouldn't erase or recreate the database instead load the data from a mysql dump
  • Since the db is populated from a dump, no fixtures should be loaded
  • Upon finishing tests the database shouldn't be destroyed

我很难让testuiterunner绕过创建。

I'm having a hard time getting the testsuiterunner to bypass creation.

推荐答案

快进到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.

这篇关于Django测试使用现有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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