Django:如何动态地创建一个模型来进行测试 [英] Django: How to create a model dynamically just for testing

查看:134
本文介绍了Django:如何动态地创建一个模型来进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django应用程序需要一个设置属性,形式如下:

 code> RELATED_MODELS =('appname1.modelname1.attribute1',
'appname1.modelname2.attribute2',
'appname2.modelname3.attribute3',...)

然后根据 attributeN 定义。



我想测试这个行为,测试应该可以工作,即使这个应用程序是项目中唯一的(除了自己的依赖项,没有其他的包装应用程序需要安装)。如何为测试数据库创建和附加/注册/激活模拟模型? (或者根本就可以吗?)



允许我使用测试夹具的解决方案将是很棒的。

解决方案

您可以将测试放在应用程序的 tests / 子目录中(而不是 tests.py 文件),并包含一个 tests / models.py 与仅测试模型。



然后提供一个测试运行脚本(示例),其中包含您在 INSTALLED_APPS 中的 tests / app。 (这在从真实项目运行应用程序测试时不起作用,这不会在 INSTALLED_APPS 中具有测试应用程序,但是我很少发现运行可重用的应用程序测试很有用来自项目,Django 1.6+不是默认的。)



注意:下面描述的替代动态方法仅适用于Django 1.1+如果您的测试用例子类 TransactionTestCase - 这显着减慢了测试速度,并且在Django 1.7+中完全不再起作用,仅仅出于历史意义,在开始测试(即在setUp方法中,或在一组doctests开始时),您可以动态添加<$($)

c $ c>myapp.tests到INSTALLED_APPS设置,然后执行以下操作:

 从django.core.management导入call_command 
从django.db.models导入加载
load.cache.loaded = False
call_command('syncdb',verbosity = 0)

然后在测试结束时,您应该通过恢复旧版本的INSTALLED_APPS并清除应用程序缓存来清理。



此类封装了该模式,因此不会像您的测试代码那样混乱很多。


I have a Django app that requires a settings attribute in the form of:

RELATED_MODELS = ('appname1.modelname1.attribute1',
                  'appname1.modelname2.attribute2', 
                  'appname2.modelname3.attribute3', ...)

Then hooks their post_save signal to update some other fixed model depending on the attributeN defined.

I would like to test this behaviour and tests should work even if this app is the only one in the project (except for its own dependencies, no other wrapper app need to be installed). How can I create and attach/register/activate mock models just for the test database? (or is it possible at all?)

Solutions that allow me to use test fixtures would be great.

解决方案

You can put your tests in a tests/ subdirectory of the app (rather than a tests.py file), and include a tests/models.py with the test-only models.

Then provide a test-running script (example) that includes your tests/ "app" in INSTALLED_APPS. (This doesn't work when running app tests from a real project, which won't have the tests app in INSTALLED_APPS, but I rarely find it useful to run reusable app tests from a project, and Django 1.6+ doesn't by default.)

(NOTE: The alternative dynamic method described below only works in Django 1.1+ if your test case subclasses TransactionTestCase - which slows down your tests significantly - and no longer works at all in Django 1.7+. It's left here only for historical interest; don't use it.)

At the beginning of your tests (i.e. in a setUp method, or at the beginning of a set of doctests), you can dynamically add "myapp.tests" to the INSTALLED_APPS setting, and then do this:

from django.core.management import call_command
from django.db.models import loading
loading.cache.loaded = False
call_command('syncdb', verbosity=0)

Then at the end of your tests, you should clean up by restoring the old version of INSTALLED_APPS and clearing the app cache again.

This class encapsulates the pattern so it doesn't clutter up your test code quite as much.

这篇关于Django:如何动态地创建一个模型来进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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