我的 Django 单元测试需要很长时间才能运行,所以我正在寻找加快速度的方法.我正在考虑安装 SSD,但我知道这也有其缺点.当然,我可以用我的代码做一些事情,但我正在寻找结构修复.即使运行单个测试也很慢,因为每次都需要重建/南迁移数据库.所以这是我的想法...
My Django unit tests take a long time to run, so I'm looking for ways to speed that up. I'm considering installing an SSD, but I know that has its downsides too. Of course, there are things I could do with my code, but I'm looking for a structural fix. Even running a single test is slow since the database needs to be rebuilt / south migrated every time. So here's my idea...
既然我知道测试数据库总是很小,为什么我不能将系统配置为始终将整个测试数据库保存在 RAM 中?切勿触摸磁盘.我如何在 Django 中配置它?我更愿意继续使用 MySQL 因为那是我在生产中使用的,但是如果 SQLite 3 或其他使这变得容易的东西,我会那样做.
Since I know the test database will always be quite small, why can't I just configure the system to always keep the entire test database in RAM? Never touch the disk at all. How do I configure this in Django? I'd prefer to keep using MySQL since that's what I use in production, but if SQLite 3 or something else makes this easy, I'd go that way.
SQLite 或 MySQL 是否可以选择完全在内存中运行?应该可以配置一个 RAM 磁盘,然后配置测试数据库以将其数据存储在那里,但我不确定如何告诉 Django/MySQL 为某个数据库使用不同的数据目录,尤其是因为它不断被擦除并重新创建每次运行.(我使用的是 Mac FWIW.)
Does SQLite or MySQL have an option to run entirely in memory? It should be possible to configure a RAM disk and then configure the test database to store its data there, but I'm not sure how to tell Django / MySQL to use a different data directory for a certain database, especially since it keeps getting erased and recreated each run. (I'm on a Mac FWIW.)
如果在运行测试时将数据库引擎设置为 sqlite3,Django 将使用内存数据库.
If you set your database engine to sqlite3 when you run your tests, Django will use a in-memory database.
我在我的 settings.py
中使用这样的代码在运行我的测试时将引擎设置为 sqlite:
I'm using code like this in my settings.py
to set the engine to sqlite when running my tests:
if 'test' in sys.argv:
DATABASE_ENGINE = 'sqlite3'
或者在 Django 1.2 中:
Or in Django 1.2:
if 'test' in sys.argv:
DATABASES['default'] = {'ENGINE': 'sqlite3'}
最后在 Django 1.3 和 1.4 中:
And finally in Django 1.3 and 1.4:
if 'test' in sys.argv:
DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}
(在 Django 1.3 中,后端的完整路径并不是必需的,但可以使设置向前兼容.)
(The full path to the backend isn't strictly necessary with Django 1.3, but makes the setting forward compatible.)
您还可以添加以下行,以防您遇到南迁移问题:
You can also add the following line, in case you are having problems with South migrations:
SOUTH_TESTS_MIGRATE = False
这篇关于如何仅在内存中运行 Django 的测试数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!