Django:如何动态创建模型仅用于测试 [英] Django: How to create a model dynamically just for testing

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

问题描述

我有一个需要 settings 属性的 Django 应用,其形式为:

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', ...)

然后根据定义的 attributeN 钩住他们的 post_save 信号以更新一些其他固定模型.

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.

推荐答案

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

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.

然后提供一个测试运行脚本(example) 在 INSTALLED_APPS 中包含您的 tests/ 应用程序".(从实际项目运行应用程序测试时,这不起作用,INSTALLED_APPS 中没有测试应用程序,但我很少发现从项目和 Django 运行可重用的应用程序测试有用1.6+ 不默认.)

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

(注意:下面描述的替代动态方法仅适用于 Django 1.1+,如果您的测试用例子类 TransactionTestCase - 这会显着减慢您的测试速度 - 并且不再有效在 Django 1.7+ 中完全没有.它留在这里只是为了历史兴趣;不要使用它.)

(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.)

在您的测试开始时(即在 setUp 方法中,或在一组 doctests 的开头),您可以将 "myapp.tests" 动态添加到 INSTALLED_APPS 设置中,然后这样做:

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)

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

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