在 Django 中测试特定模型

发布于 2024-10-31 19:04:15 字数 218 浏览 0 评论 0原文

是否可以有一组模型仅用于测试目的?我的想法是,我编写了一个应用程序,其中包含一些辅助抽象模型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2024-11-07 19:04:15

您可以尝试创建一个仅在开发服务器上使用的全新应用程序。

例如,如果您的应用程序名为 myapp,您将调用您的测试应用程序 myapp_test

然后在 myapp_testmodels.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 app myapp_test.

Then in myapp_test's models.py you would from myapp import models and then subclass your models in there.

Then in your settings.py you either just try and remember to comment out the myapp_test application from INSTALLED_APPS when deploying to your production server. Or you can use the local_settings.py methodology to only have the myapp_test included in INSTALLED_APPS on your test machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文