Django test (nose) is accelerated using reuse_db

I use django nose to run my unit tests in django (1.4).

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' 

Creating a database takes a lot of time.

So, I found myself putting this in settings.py:

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

gotta do the trick.

django itsellve actually 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 each database change) using this flag = 0

However, when you set the flag to 0, my tests end up with a 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 that the table does not exist

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

The test runs fine if reuse_db is set to 0

I use 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 do not use the sqlite database in memory for testing, because I read that this does not work with django-nose.

So how can I reuse the database when it destroys the database at the end ...

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

+4
source share
3 answers

If I understand correctly, you do not know how to create a test database for the first time (to reuse it later).

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

test_db_settings.py

in which you indicate:

 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 a database that is used by TestRunner. We can run the test:

 REUSE_DB=1 python manage.py test 

Update

Are you sure you are using NoseTestSuiteRunner? Here is the code from django_nose.NoseTestSuiteRunner. Since we can see if the REUSE_DB option is set, teardown_database is disabled. If you want, you can debug it, for example, set a breakpoint here to check if 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 
+9
source

I accepted the answer of Andrey Kaygorodov because he led me to a decision ... after reading his answer, I thought (how easy and stupid of me ...)

In any case, put this settings_test_db.py parameter next to the settings with this content: (filling in the name of your project)

 from <myproject>.settings import * DATABASES = { 'default': { 'NAME': os.path.join(BUILDOUT_DIR, 'var', 'sqlite', 'unittest.db'), 'ENGINE': 'django.db.backends.sqlite3', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } } 

And for sqlite you do not need to create db. just run

 python manage.py syncdb --settings <myproject>.settings_test_db 

(note: use the project name without .py)

and perform migrations

 python manage.py migrate --settings <myproject>.settings_test_db 

(only if you use South)

and set in your settings.py:

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

so you can use

 python manage.py test 
+2
source

By the way, instead of using a separate test settings file ... you can define a test Anna in the same settings:

 DATABASES = { 'default': { 'NAME': os.path.join(BUILDOUT_DIR, 'var', 'sqlite', 'test.db'), 'TEST_NAME': os.path.join(BUILDOUT_DIR, 'var', 'sqlite', 'unittest.db'), 'ENGINE': 'django.db.backends.sqlite3', 'USER': '', 'PASSWORD': '', 'HOST': '', # empty string for localhost. 'PORT': '', # empty string for default. } } 
+1
source

Source: https://habr.com/ru/post/1485961/


All Articles