如何在生产数据库上运行 django 单元测试? [英] How to run django unit-tests on production database?

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

问题描述

我开始采用 TDD 开发态度,并正在为我的 Django 应用程序编写单元测试.我知道fixtures并且知道这是应该执行测试的方式,但是对于给定的测试,我确实需要在整个数据库上执行它,并且10+百万行数据库的jsonfixture不是我想要处理的,而且,这个测试是只读的".

i'm starting the TDD development attitude and am writting unit-tests for my django application. I'm aware of fixtures and know that's the way tests should be executed, but for a given test i do need to execute it on the whole database, and json fixture for 10+ million row database is not something i'd like to handle, moreover, this test is "read-only".

那么问题是您如何设置测试套件以在生产数据库上运行?我想这就像在某些测试的 setUp 方法中添加 DATABASE_NAME 设置一样简单.但是 settings.DATABASE_NAME="prod_db" 在运行测试时导致NameError: global name 'settings' is not defined".此外,http://code.djangoproject.com/ticket/11987 中描述了一个风险,您可能会意外删除生产数据库.

So the question is how are you setting up your test suites to run on the production database? I imagine it could be as easy as adding the DATABASE_NAME setting in the setUp method of certain test. But the settings.DATABASE_NAME="prod_db" results in "NameError: global name 'settings' is not defined" while running the test. Moreover, there is a risk described in http://code.djangoproject.com/ticket/11987, that you can accidentally delete a production database.

那么,在生产数据库上运行测试套件的单个测试而不是临时测试,如何可能,或者更好的是,最佳实践是什么?

So, how is it possible, or, even better, what is best practice, to run a single test of a test suite on a production database instead of temporary one?

如有任何意见,请提前干杯!

Cheers in advance for any opinions!

推荐答案

首先,如果您在生产数据库上运行它,它就不是一个单元"测试.

First, if you're running it on the production database, it isn't much of a "unit" test.

这是一流的批处理作业,需要像一流的生产批处理作业一样对待.

It's a first-class batch job and needs to be treated like a first-class production batch job.

您不能使用 Django test 命令查看生产数据.它始终创建一个空数据库,该数据库由 TestCase 中的装置填充.

You cam't to use the Django test command for looking at production data. It always creates an empty database which is populated from fixtures in the TestCase.

你可以让你的生产数据库处理适当的管理命令.这已经正确配置了环境,以便您的命令可以简单地使用 Django ORM 来处理您的数据.

You could make your production database processing a proper management command. This has the environment all properly configured so that your command can simply use the Django ORM to process your data.

另一种方法是确保您配置了设置.任何一个使用 DJANGO_SETTINGS_MODULE 环境变量或使用 settings.configure() 函数来创建环境.

The alternative is to be sure that you configure your settings. Either use the DJANGO_SETTINGS_MODULE environment variable or use the settings.configure() function to create an environment.

然后您可以导入模型并对生产数据库执行您想要执行的处理.

You can then import the models and do the processing you want to do against the production database.

如果您愿意,您可以将其称为测试",但您正在查看生产数据,因此在获取设置文件和使用正确的 ORM 配置方面,必须将其视为生产应用程序.

You can call it "test" if you want to, but you're looking at production data, so it's got to be treated like production application with respect to getting the settings file and using the proper ORM configuration.

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

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