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

查看:147
本文介绍了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中继线进行测试。

Tested it with current Django trunk.

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

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