Different db for testing in Django?

后端 未结 8 1984
轻奢々
轻奢々 2020-12-04 15:12
DATABASES = {
#    \'default\': {
#        \'ENGINE\': \'postgresql_psycopg2\',
#        ...
#    }

    # for unit tests
    \'default\': {
        \'ENGINE\': \'dj         


        
相关标签:
8条回答
  • 2020-12-04 15:45

    Though this is already solved...

    If your database for tests is just a normal DB:

    I think you are not doing unit test since you rely in the database. Anyway, django contains a test type for that (not unitary): django.test.TestCase

    You need to derive from django.test.TestCase instead of unittest.TestCase that will create a fresh rehershal database for you that will be destroyed when the test end.

    There are interesting explanations/tips about testing with db in the following link
    Testing Django Applications

    0 讨论(0)
  • 2020-12-04 15:49

    If you have access to manually create the database, you could use django-nose as your TEST_RUNNER. Once installed, if you pass the following environment variable, it will not delete and re-create the database.

    REUSE_DB=1 ./manage.py test
    

    You can also add the following to settings.py so you don't have to write REUSE_DB=1 every time you want to run tests:

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

    Note: this will also leave all your tables in the databases which means test setup will be a little quicker, but you will have to manually update the tables (or delete and re-create the database yourself) when you change your models.

    0 讨论(0)
  • 2020-12-04 15:51

    I solved this issue simply creating other settings constant DATABASES_AVAILABLE.

    DATABASES_AVAILABLE = {
        'main': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'nep',
            'USER': 'user',
            'PASSWORD': 'passwd',
            'HOST': 'localhost',
        },
        'remote': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'nes_dev',
            'USER': 'usr',
            'PASSWORD': 'passwd',
            'HOST': '200.144.254.136',
        },
        'sqlite': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        },
    }
    
    # This solves the problem with tests
    # Define a system variable called DJANGO_DATABASE_TEST and set it to the
    # the database you want
    database = os.environ.get('DJANGO_DATABASE_TEST', 'main')
    DATABASES = {
        'default': DATABASES_AVAILABLE[database]
    }
    
    0 讨论(0)
  • 2020-12-04 15:52

    The way I handle this is through having multiple settings files, since I use that to maintain a set of common settings with modifications for each instance. It's a little more complicated to set up than some of the other solutions, but I needed to do it anyway because I was managing slightly different settings for local development, remote development, staging and production.

    https://code.djangoproject.com/wiki/SplitSettings has a number of options for managing settings, and I've chosen a practice similar to the one described at https://code.djangoproject.com/wiki/SplitSettings#SimplePackageOrganizationforEnvironments

    So, in my Django project directory, I have a settings folder that looks like this:

    $ tree settings
    settings
    ├── defaults.py
    ├── dev.py
    ├── dev.pyc
    ├── __init__.py
    ├── lettuce.py
    ├── travis.py
    ├── unittest.py
    

    The common settings are in settings/defaults.py and I import these in my instance settings files. So settings/unittest.py looks like this:

    from defaults import *
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'my_database',
        }
    } 
    

    Then, when I want to run tests, I just execute:

    $ ./manage.py test --settings=settings.unittest
    

    to use sqlite for testing. I'll use a different settings module if I want to use a different test runner or database configuration.

    0 讨论(0)
  • 2020-12-04 15:55

    Why could I be getting this error?

    Because of insufficient permissions. You can alter the user permissions by ALTER USER username CREATEDB; after running psql with superuser priviliges.

    Example,

    $ sudo su - postgres
    $ psql
    psql (9.3.18)
    Type "help" for help.
    
    postgres=# ALTER USER username CREATEDB;
    ALTER ROLE
    
    0 讨论(0)
  • 2020-12-04 15:57

    In your settings.py (or local_settings.py):

    import sys
    if 'test' in sys.argv:
        DATABASES['default'] = {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'mydatabase'
        }
    
    0 讨论(0)
提交回复
热议问题