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

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

问题描述

我希望能够使用现有的测试数据库来运行我的测试,并且不想让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)绑定 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 Tests使用的数据库,而不是每次都构建它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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