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

时间:2022-07-30 07:21:05

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.

运行Django单元测试太慢了。特别是当我只想运行一个测试但是测试运行器想要创建整个数据库并且为了那个测试而销毁整个数据库时。

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.

在我没有改变任何模型的情况下,如果Django不打算尝试创建和销毁整个数据库,我可以节省大量时间,而是将其保存下次。更好的是,如果测试运行器能够看到哪些模型已经更改并且仅在运行测试之前替换那些模型,那将是很好的。

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?

我宁愿不必自己将测试运行器子类化,但如果我很快找不到解决方案,那就是我将要做的事情。有没有这样的东西已经存在?

4 个解决方案

#1


11  

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

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

./manage.py test --keepdb

#2


6  

django-test-utils provides Persistent Database Test Runner functionality, http://django-test-utils.readthedocs.org/en/latest/keep_database_runner.html

django-test-utils提供持久数据库测试运行器功能,http://django-test-utils.readthedocs.org/en/latest/keep_database_runner.html

#3


4  

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

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

#4


1  

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.

我正在使用Djang-nose。如果设置env var REUSE_DB = 1,则在运行测试后不会销毁数据库,并在下次运行时重新使用相同的数据库。每当架构发生变化时,只需设置REUSE_DB = 0并执行一次“完整”运行。之后将其重置为1并且你很高兴。

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

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

#1


11  

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

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

./manage.py test --keepdb

#2


6  

django-test-utils provides Persistent Database Test Runner functionality, http://django-test-utils.readthedocs.org/en/latest/keep_database_runner.html

django-test-utils提供持久数据库测试运行器功能,http://django-test-utils.readthedocs.org/en/latest/keep_database_runner.html

#3


4  

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

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

#4


1  

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.

我正在使用Djang-nose。如果设置env var REUSE_DB = 1,则在运行测试后不会销毁数据库,并在下次运行时重新使用相同的数据库。每当架构发生变化时,只需设置REUSE_DB = 0并执行一次“完整”运行。之后将其重置为1并且你很高兴。

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

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