Django:单元测试抽象模型的最佳方式 [英] Django: Best way to unit-test an abstract model

查看:25
本文介绍了Django:单元测试抽象模型的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为抽象的基础模型编写一些单元测试,它提供了一些其他应用程序应该使用的基本功能.有必要定义一个继承自它的模型仅用于测试目的;是否有任何优雅/简单的方法来定义该模型仅用于测试?

I need to write some unit tests for an abstract base model, that provides some basic functionality that should be used by other apps. It it would be necessary to define a model that inherits from it just for testing purposes; are there any elegant/simple ways to define that model just for testing?

我见过一些黑客"使这成为可能,但从未在 django 文档或其他类似地方看到过官方"方式.

I have seen some "hacks" that make this possible, but never seen an "official" way in the django documentation or in other similar places.

推荐答案

我自己刚刚偶然发现了这个功能:您可以从 tests.py 中的抽象模型继承并照常进行测试.当您运行manage.py tests"时,Django 不仅会创建一个测试数据库,还会验证 &同步您的测试模型.

Just stumbled across this feature myself: You can just inherit from your abstract model in tests.py and test that as usual. When you run 'manage.py tests', Django not only creates a test database, but also validates & syncs your test models.

使用当前的 Django 主干(版本 1.2)对其进行了测试.

Tested it with current Django trunk (version 1.2).

这篇关于Django:单元测试抽象模型的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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