如何为 Django 测试指定要使用的数据库,而不是每次都构建它? [英] How can I specify a database for Django Tests to use instead of having it build it every time?

查看:32
本文介绍了如何为 Django 测试指定要使用的数据库,而不是每次都构建它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用现有的测试数据库来运行我的测试,而不是每次我想运行测试时都让 Django 创建和删除数据库.这可能吗?

I want to be able to use an existing test database to run my tests against and not have Django create and delete a database every time I want to run the tests. Is this possible?

推荐答案

有可能,这里有一个方法:

It's possible, here is a way :

1) 定义您自己的测试运行程序外观 此处 了解操作方法.

1) Define your own test runner look here to see how.

2) 对于您的自定义测试运行程序,请查看 默认测试运行器,你可以复制并粘贴代码,然后注释这一行:connection.creation.destroy_test_db(old_name,verbosity),它负责销毁测试数据库,我认为您应该尝试使用 connection.creation.create_test_db(..) 行,除非可能是这样的:

2) For your custom test runner look in the default test runner, you can just copy and past the code and just comment this line : connection.creation.destroy_test_db(old_name, verbosity) which is responsible for destroying the test database, and i think you should put the connection.creation.create_test_db(..) line in a try except something like this maybe:

try:
    # Create the database the first time.
    connection.creation.create_test_db(verbosity, autoclobber=not interactive) 
except ..: # Look at the error that this will raise when create a database that already exist
    # Test database already created.
    pass 

3) 在 setting.py 中绑定 TEST_RUNNER给您的测试运行器.

3) Bound TEST_RUNNER in setting.py to your test runner.

4) 现在像这样运行你的测试:./manage.py test

4) Now run your test like this: ./manage.py test

这篇关于如何为 Django 测试指定要使用的数据库,而不是每次都构建它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆