2. 如何通过在测试运行之间重用数据库来加快测试速度?

When we execute the command python manage.py test, a new db is created everytime. This doesn’t matter much if we don’t have many migrations.

But when we have many migrations, it takes a long time to recreate the database between the test runs. To avoid such situations, we may reuse the old database.

您可以通过在测试命令中添加-keepdb标志来防止破坏测试数据库。 This will preserve the test database between runs. If the database does not exist, it will first be created. If any migrations have been added since the last test run, they will be applied in order to keep it up to date.

$ python manage.py test --keepdb