Django 测试使用现有数据库

标签 django django-testing

我很难自定义测试数据库设置行为。我想实现以下目标:

  • 测试套件需要使用现有数据库
  • 测试套件不应删除或重​​新创建数据库,而是从 mysql 转储
  • 加载数据
  • 由于数据库是从转储中填充的,因此不应加载任何固定装置
  • 完成测试后,不应破坏数据库

  • 我很难让 testsuiterunner 绕过创建。

    最佳答案

    快进到 2016 年,在 django 中内置了在测试之间保留数据库的能力。它以 --keep flag 的形式提供。管理.py

    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.


    这几乎满足了您在问题中提到的所有标准。事实上,它甚至更进一步。无需在每次运行之前导入转储。

    关于Django 测试使用现有数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6250353/

    相关文章:

    python - 如何使用 Heroku 配置 Django 数据库?

    python - 将 "fake"模型条目添加到管理索引

    django - django_webtest 中的用户身份验证

    python - 如何跨 django 测试函数添加/更改变量

    Django 脆皮表单可以与自定义小部件一起使用吗?

    django 获取当前用户 ID

    django - 在 django-react-redux 中 PUT 文件请求

    django - 在 Django 中测试页面内容中的链接

    django - 在 Django 中模拟模型方法