在 Django 中测试特定模型
是否可以有一组模型仅用于测试目的?我的想法是,我编写了一个应用程序,其中包含一些辅助抽象模型 HelperBase。现在我想提供一些继承它的模型来测试它,例如 DerivedTest1、DerivedTest2。然而,我真的不希望这些测试模型最终出现在生产数据库中。我只想在测试数据库中构建他们的表。是否可能,如果可以-如何做到?我已经尝试在 tests.py
文件中创建模型,但这似乎不起作用。
Is it possible to have a set of models just for testing purposes? The idea is that I've written an app that contains some helper abstract model HelperBase. Now I'd like to provide some models that would inherit from it in order to test it, say DerivedTest1, DerivedTest2. However I wouldn't really like those test models to appear in the production database in the end. I just want their tables to be constructed in the test database. Is it possible and if so - how to do it? I've already tried creating models in the tests.py
file but this doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试创建一个仅在开发服务器上使用的全新应用程序。
例如,如果您的应用程序名为
myapp
,您将调用您的测试应用程序myapp_test
。然后在
myapp_test
的models.py
中,您将from myapp import models
,然后在其中对模型进行子类化。然后,在您的
settings.py
中,您可以在部署到生产服务器时尝试并记住注释掉INSTALLED_APPS
中的myapp_test
应用程序。或者,您可以使用local_settings.py
方法仅将myapp_test
包含在测试计算机上的INSTALLED_APPS
中。You could try creating a whole new app that you only use on your development server.
E.g., if your app is called
myapp
you would call your testing appmyapp_test
.Then in
myapp_test
'smodels.py
you wouldfrom myapp import models
and then subclass your models in there.Then in your
settings.py
you either just try and remember to comment out themyapp_test
application fromINSTALLED_APPS
when deploying to your production server. Or you can use thelocal_settings.py
methodology to only have themyapp_test
included inINSTALLED_APPS
on your test machine.