如何在我的生产数据库副本上运行Django测试?

时间:2022-03-02 07:31:42

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

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

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
  • 运行manage.py dumpdata -o app.dump
  • Move the resulting app.dump file to a fixtures directory in the [app name] folder
  • 将生成的app.dump文件移动到[app name]文件夹中的fixtures目录
  • Specify a 'fixtures' class attribute on my django.test.TestCase subclass
  • 在我的django.test.TestCase子类上指定'fixtures'类属性

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.

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

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

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

2 个解决方案

#1


3  

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.

通常,不鼓励对实时DB或实时DB的副本进行测试。为什么?因为测试需要是可预测的。当您复制实时数据库时,输入变得不可预测。第二个问题是您无法在现场测试,因此您需要克隆数据。对于超过几MB的任何东西来说,这都是缓慢的。

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.

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

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.

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

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中提供的低级连接对象来执行,因此可以用于加载夹具。

#2


0  

You didn't mention which version of Django you're using, but looking at the 1.11 documentation:

您没有提到您正在使用的Django版本,但查看了1.11文档:

It's not clear from the 1.11 docs about fixture loading for tests whether they would also look in FIXTURE_DIRS, though. So this might not solve your problem entirely.

然而,从1.11文档中不清楚测试的夹具加载是否也会在FIXTURE_DIRS中查看。所以这可能无法完全解决您的问题。

#1


3  

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.

通常,不鼓励对实时DB或实时DB的副本进行测试。为什么?因为测试需要是可预测的。当您复制实时数据库时,输入变得不可预测。第二个问题是您无法在现场测试,因此您需要克隆数据。对于超过几MB的任何东西来说,这都是缓慢的。

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.

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

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.

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

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中提供的低级连接对象来执行,因此可以用于加载夹具。

#2


0  

You didn't mention which version of Django you're using, but looking at the 1.11 documentation:

您没有提到您正在使用的Django版本,但查看了1.11文档:

It's not clear from the 1.11 docs about fixture loading for tests whether they would also look in FIXTURE_DIRS, though. So this might not solve your problem entirely.

然而,从1.11文档中不清楚测试的夹具加载是否也会在FIXTURE_DIRS中查看。所以这可能无法完全解决您的问题。