我如何告诉 Django 保存我的测试数据库?
运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 django1.8 中,为
manage.py test
命令添加了新参数 --keepdbIn django1.8 added new parameter for
manage.py test
command --keepdb您是否尝试过使用内存 SQLite 数据库进行测试?它比使用基于磁盘的数据库要快得多。
Have you tried using an in-memory SQLite database for tests? It's much faster than using a disk-based database.
我用的是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