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

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

问题描述

我正在开始TDD开发态度,并为我的django应用程序编写单元测试。我知道灯具,知道这是测试应该执行的方式,但是对于给定的测试,我需要在整个数据库上执行它,而对于10多万行数据库来说,json fixture不是我想要处理的此外,此测试是只读。



所以问题是您如何设置测试套件以在生产数据库上运行?我想像在某些测试的setUp方法中添加DATABASE_NAME设置可能很简单。但是在运行测试时,settings.DATABASE_NAME =prod_db会导致NameError:global name设置未被定义。而且, http://code.djangoproject.com/ticket/11987 中描述的风险,您可以意外删除生产数据库。那么,在生产数据库上运行一个测试套件的单一测试而不是临时的测试,那么,怎么可能,甚至更好的是什么是最佳实践呢?



提前提醒任何意见!

解决方案

你正在生产数据库上运行它,这不是一个单位测试。



这是一个一流的批处理工作,需要像一个一流的生产批处理作业。



你不会使用Django test 命令查看生产数据。它总是创建一个从TestCase中的灯具填充的空数据库。



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



替代方法是确保您配置您的设置
使用 DJANGO_SETTINGS_MODULE 环境变量,或使用 settings.configure()函数创建一个环境。



然后,您可以导入模型,然后对生产数据库进行处理。



您可以将其称为测试,但您正在查看生产数据,因此必须将其视为生产应用程序,以获取设置文件并使用正确的ORM配置。


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".

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.

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.

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.

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.

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