Django(鼻子)测试加速与reuse_db不工作 [英] Django (nose) test speeding up with reuse_db not working

查看:182
本文介绍了Django(鼻子)测试加速与reuse_db不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用django-nose在django(1.4)中运行我的单元测试。

  TEST_RUNNER ='django_nose.NoseTestSuiteRunner '

创建数据库需要很长时间。



所以我发现把它放在settings.py中:

  os.environ ['REUSE_DB'] =1 

应该做的伎俩。



django itellve给出这个建议:

 要重用旧数据库<路径不是很有趣> /var/sqlite/unittest.db 为了速度,设置env var REUSE_DB = 1。 

当然,您需要运行一次(或每个数据库更改后),此标志= 0



但是,当您将标志设置为0时,我的测试结束语为:

 code>销毁别名default的测试数据库... 

所以当我想运行它与重用....没有什么可以重用...我会收到错误说表不存在

  DatabaseError:没有这样的表:<和表名> 

将reuse_db设置为0时,测试运行完美



我在开发设置中使用测试数据库别名:

  DATABASES = {
'default' :{
'NAME':os.path.join(BUILDOUT_DIR,'var','sqlite','development.db'),
'TEST_NAME':os.path.join(BUILDOUT_DIR, var','sqlite','unittest.db'),
'ENGINE':'django.db.backends.sqlite3',
'USER':'',
'PASSWORD' :'',
'HOST':'',
'PORT':'',
}
}

我没有使用内存中的sqllite数据库进行测试,因为我读到某个地方不适用于django-nose。



那么在最终破坏数据库的时候,怎么可以重用数据库?



根据这个 https://docs.djangoproject.com/en/1.4/topics/testing/#the-test-database django正在这样做,但是它不显示如何防止这个(如果可以),或者如何使用reuse_db选项。
我应该使用其他设置吗?

解决方案

如果我理解正确,你不知道如何创建测试数据库第一次(以便稍后重用)。



如果DB不存在,则NoseTestSuiteRunner将自动创建,即使您设置REUSE_DB = 0。
如果要手动创建测试数据库,您可以创建以下文件:



test_db_settings.py



其中您指定:

 从设置导入* 

DATABASES = {
'默认': {
'ENGINE':'django.db.backends.postgresql_psycopg2',
#TestRunner使用名称为
#的前缀test_和您的数据库名称的数据库,如果您的数据库
#name是db,那么测试数据库名称必须是test_db
'NAME':'test_db',
'USER':'postgres_user',
'PASSWORD':'postgres_user ',
'HOST':'localhost',
'PORT':'5432',
}
}

之后创建test_db:

  createdb -U postgres_user -h localhost test_db#如果你使用postgres 

python manage.py syncdb --settings test_db_settings.py
python manage.py migrate --settings test_db_settings.py(仅当你使用南方)

现在我们有TestRunner使用的DB。我们可以运行测试:

  REUSE_DB = 1 python manage.py test 
pre>

更新



您确定使用NoseTestSuiteRunner吗?这是django_nose.NoseTestSuiteRunner中的一些代码。我们可以看到如果选项REUSE_DB被设置,则teardown_database被禁用。如果你想要你可以调试它,例如在这里设置一个断点来检查你是否真的使用其Runner等。

  def teardown_databases(self ,* args,** kwargs):
如果REUSE_DB为true,则将这些可重复使用的数据库单独保留。
如果不是_reusing_db():
返回超级(NoseTestSuiteRunner,自己).teardown_databases(
* args,** kwargs)
#else跳过数据库,以便下次再次使用


I am using django-nose to run my unit tests in django (1.4).

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

Creating the database takes a long time.

So I found out putting this in settings.py:

os.environ['REUSE_DB'] = "1"

should do the trick.

actually django itsellve gives this suggestion:

To reuse old database "<path not very interesting>/var/sqlite/unittest.db" for speed, set env var REUSE_DB=1.

of course you need to run it once (or after every database change) with this flag =0

However, when you set the flag to 0, my tests end with the remark:

Destroying test database for alias 'default'...

So when I want to run it with reuse.... there is nothing to reuse... and I will get errors saying the table does not exist

DatabaseError: no such table: <and than a table name>

The test runs perfectly when set the reuse_db to 0

I am using the test database alias in my development settings:

DATABASES = {
    'default': {
        'NAME': os.path.join(BUILDOUT_DIR, 'var', 'sqlite', 'development.db'),
        'TEST_NAME': os.path.join(BUILDOUT_DIR, 'var', 'sqlite', 'unittest.db'),
        'ENGINE': 'django.db.backends.sqlite3', 
        'USER': '',
        'PASSWORD': '',
        'HOST': '', 
        'PORT': '', 
        }
    }

I am not using the in-memory sqllite database for testing because I read somewhere this doesn't work well with django-nose.

So how can I reuse the DB when it is destroying the databse in the end...

according to this https://docs.djangoproject.com/en/1.4/topics/testing/#the-test-database django is doing this, but it does not show how to prevent this (if I can), or how to use the reuse_db option. should I use other settings?

解决方案

If I have understood correctly, you don't know how to create the test database first time (in order to reuse it later).

NoseTestSuiteRunner should create it automatically if DB does not exist even if you set REUSE_DB = 0. If you want to create test DB manually you can create the following file:

test_db_settings.py

in which you specify:

from settings import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        # TestRunner uses a database with name that consists
        # of prefix test_ and your database name if your database
        # name is db then the test database name has to be test_db
        'NAME': 'test_db', 
        'USER': 'postgres_user',
        'PASSWORD': 'postgres_user',
        'HOST': 'localhost',
        'PORT': '5432',
        }
}

after that create test_db:

createdb -U postgres_user -h localhost test_db # if you use postgres

python manage.py syncdb --settings test_db_settings.py
python manage.py migrate --settings test_db_settings.py (only if you use South)

Now we have DB that is used by TestRunner. We may run test:

REUSE_DB=1 python manage.py test

Updated

Are you sure that you use NoseTestSuiteRunner? Here is some code from django_nose.NoseTestSuiteRunner. As we can see if option REUSE_DB is set then teardown_database is disabled. If you want you can debug it for example set here a breakpoint to check you really use its Runner etc.

def teardown_databases(self, *args, **kwargs):
    """Leave those poor, reusable databases alone if REUSE_DB is true."""
    if not _reusing_db():
        return super(NoseTestSuiteRunner, self).teardown_databases(
                *args, **kwargs)
    # else skip tearing down the DB so we can reuse it next time

这篇关于Django(鼻子)测试加速与reuse_db不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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