如何测试Django自定义模型字段? [英] How to test Django custom model fields?

查看:71
本文介绍了如何测试Django自定义模型字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑创建一些子类化的Django模型字段并将它们作为软件包分发到 PyPI 上。我喜欢为我的代码编写单元测试(如 TDD ),但是我

I am thinking of creating some subclassed Django model fields and distributing them as a package on PyPI. I like to write unit tests for my code (à la TDD), but I'm a bit puzzled as to how I'd write tests for this particular library.

我想到的第一个想法是创建一个利用我的Django项目的Django项目。子类,只需使用 Django测试工具,但这看起来并不十分优雅。有一种更好的方法!

The first thought that came to my mind was to create a Django project that makes use of my subclasses and just use the Django test tools, but that doesn't seem very elegant at all. There's got to be a better way!

是否有某种方法可以以这种方式引导Django?我会很高兴有人指出我正确的方向。谢谢!

Is there a method of somehow bootstrapping Django for this type of thing? I'd appreciate someone pointing me in the right direction. Thanks!

推荐答案

Django本身提供了一些用于字段子类化的测试。测试具有自己的 models.py ,其中使用了自定义字段。当您查看实际时,应该会得到最好的印象代码自己!

Django itself comes with some tests for field-subclassing; the tests have their own models.py where the custom fields are used. You should get the best impression when you have a look at the actual code yourself!

添加:django可以在 test 包中定义模型您必须将 yourapp.test 软件包添加到 INSTALLED_APPS 中。
Django本身具有内置机制自动发现自己的测试并将其添加到 INSTALLED_APPS

Addition: To have the models defined in your test package being discovered by django you will have to add your yourapp.test package to INSTALLED_APPS. Django itself has a built-in mechanism for its own tests to be automatically discovered and added to INSTALLED_APPS.

这篇关于如何测试Django自定义模型字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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