如何在我的生产数据库副本上运行 Django 测试? [英] How do I run Django tests on a copy of my production database?

查看:15
本文介绍了如何在我的生产数据库副本上运行 Django 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的 Django 应用编写了一系列测试,并希望在我的生产数据库副本上运行它们.

I've written a series of tests for my Django app, and would like to run them on a copy of my production database.

据我所知,最好的方法是使用 夹具加载 像这样:

As far as I can tell, the best way to do this is using fixture loading like so:

  • 运行 manage.py dumpdata -o app.dump
  • 将生成的 app.dump 文件移动到 [app name] 文件夹中的 fixtures 目录
  • 在我的 django.test.TestCase 子类上指定一个fixtures"类属性

但是,这种方法很麻烦.我有多个应用程序,为每个应用程序运行 manage.py dumpdata 并在每次我想测试我的应用程序时手动移动设备文件是很痛苦的.

However, this approach is cumbersome. I have multiple apps, and running manage.py dumpdata for each of them and manually moving around fixtures files every time I want to test my app is a pain.

有没有更简单的方法来自动生成整个生产数据库的副本并针对它测试我的 Django 应用程序?

Is there an easier way to automatically generate a copy of my entire production database and test my Django apps against it?

推荐答案

一般来说,不鼓励对 live DB 或 live DB 的副本进行测试.为什么?因为测试需要是可预测的.当您制作实时数据库的副本时,输入变得不可预测.第二个问题是你不能明显地在现场测试,所以你需要克隆数据.对于大小超过几 MB 的东西来说,这很慢.

Generally, testing against the live DB or a copy of the live DB is discouraged. Why? Because tests need to be predictable. When you make a copy of the live db, the input becomes unpredictable. The second problem is that you cannot obviously test on the live site, so you need to clone the data. That's slow for anything more than few MB in size.

即使数据库很小,dumpdata 后跟 loaddata 也不是办法.这是因为默认情况下,dumpdata 以 JSON 格式导出,生成开销很大,更不用说使数据文件非常庞大.使用 loaddata 导入更慢.

Even if the DB is small, dumpdata followed by loaddata isn't the way. That's because dumpdata by default exports in a JSON format which has a large generating overhead, not to mention making the data file very bulky. Importing using loaddata is even slower.

进行克隆的唯一现实方法是使用内置导出/导入机制的数据库引擎.在 sqlite 的情况下,它只是复制 db 文件.对于 mysql,它是 SELECT INTO OUTFILE,然后是 LOAD DATA INFILE.对于 postgresql,它是 COPY TO 后跟 COPY FROM 等等.

The only realistic way to make a clone is using the database engines built in export/import mechanism. In the case of sqlite that's just copying the db file. For mysql it's SELECT INTO OUTFILE followed by LOAD DATA INFILE. And for postgresql it's COPY TO followed by COPY FROM and so on.

所有这些导出/导入命令都可以使用 django 中可用的低级连接对象执行,因此可用于加载固定装置.

All of these export/import commands can be executed using the lowlevel connection object available in django and thus can be used to load fixtures.

这篇关于如何在我的生产数据库副本上运行 Django 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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