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

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

问题描述

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



尽可能的告诉我们,最好的方法是使用装载加载如此:




  • 运行manage.py dumpdata -o app.dump

  • 移动生成的app.dump文件到[app name]文件夹中的fixtures目录

  • 在我的django.test.TestCase子类中指定一个'fixtures'类属性



然而,这种方法很麻烦。我有多个应用程序,并为每个应用程序运行manage.py dumpdata,并手动移动夹具文件,每次我想测试我的应用程序是一个痛苦。



是否一个更简单的方法来自动生成我的整个生产数据库的副本,并测试我的Django应用程序?

解决方案

不鼓励实时DB或活动DB的副本。为什么?因为测试需要可预测。当您制作活动数据库的副本时,输入将变得不可预测。第二个问题是您无法在实时站点上进行测试,因此您需要克隆数据。对于任何超过几MB的大小,这是很慢的。



即使DB很小, dumpdata code> loaddata 不是这样。这是因为dumpdata默认以JSON格式导出,具有很大的生成开销,更不用说使数据文件非常庞大。使用loaddata导入更慢。



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



所有这些导出/导入命令都可以使用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:

  • Run manage.py dumpdata -o app.dump
  • Move the resulting app.dump file to a fixtures directory in the [app name] folder
  • Specify a 'fixtures' class attribute on my django.test.TestCase subclass

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.

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

解决方案

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.

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.

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.

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天全站免登陆