如何为Django测试指定一个数据库,而不是每次都建立一个数据库? [英] How can I specify a database for Django Tests to use instead of having it build it everytime?

查看:987
本文介绍了如何为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 everytime I want to run the tests. Is this possible?

推荐答案

这可能是这样的:

1)定义你自己的测试运行器,看看这里查看如何。

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

2)对于您的自定义测试运行器,请查看默认测试运行器,您可以只复制代码并将代码注释到以下行: connection.creation.destroy_test_db(old_name ,详细)它负责销毁测试数据库,我认为你应该把 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)绑定 TEST_RUNNER ,将settings.py设置为测试运行器。

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天全站免登陆