我如何告诉 Django 保存我的测试数据库?

发布于 2024-09-07 10:00:38 字数 274 浏览 2 评论 0原文

运行 Django 单元测试太慢了。特别是当我只想运行一项测试,但测试运行者想要创建整个数据库并仅为该一项测试销毁整个数据库时。

在我没有更改任何模型的情况下,如果 Django 不费心尝试创建和销毁整个数据库,而是将其保存以供下次使用,我可以节省大量时间。更好的是,如果测试运行者能够看到哪些模型发生了变化并且只在运行测试之前替换那些模型,那就太好了。

我不想自己对测试运行程序进行子类化,但如果我不能很快找到解决方案,这就是我必须要做的。有这样的东西已经存在吗?

Running Django unit tests is far too slow. Especially when I just want to run one test but the test runner wants to create the entire database and destroy the whole thing just for that one test.

In the case where I have not changed any of my models, I could save oodles of time if Django would not bother trying to create and destroy the entire database, and instead saved it for next time. Better yet, it would be great if the test runner was capable of being able to see which models have changed and only replacing those prior to running tests.

I'd prefer to not have to subclass the test runner myself, but that's what I'm going to have to do if I don't find a solution soon. is there anything like this already in existence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

始终不够爱げ你 2024-09-14 10:00:39

在 django1.8 中,为 manage.py test 命令添加了新参数 --keepdb

./manage.py test --keepdb

In django1.8 added new parameter for manage.py test command --keepdb

./manage.py test --keepdb
绻影浮沉 2024-09-14 10:00:39

您是否尝试过使用内存 SQLite 数据库进行测试?它比使用基于磁盘的数据库要快得多。

Have you tried using an in-memory SQLite database for tests? It's much faster than using a disk-based database.

愁以何悠 2024-09-14 10:00:39

我用的是Djang-nose。如果您设置环境变量 REUSE_DB=1,它不会在运行测试后销毁数据库,并在下次运行时重新使用同一数据库。每当您的架构发生更改时,只需设置 REUSE_DB=0 并执行一次“完整”运行。之后将其重置为 1 就可以了。

https://github.com/django-nose/django-nose

I'm using Djang-nose. If you set a env var REUSE_DB=1 it will not destroy the DB after running tests and re-use that same DB for the next run. Whenever your schema changes, just set REUSE_DB=0 and do one 'full' run. After that reset it to 1 and you're good to go.

https://github.com/django-nose/django-nose

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