Different db for testing in Django?

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

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


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

    You can specify test database in settings.py. See https://docs.djangoproject.com/en/3.0/topics/testing/overview/#the-test-database

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'USER': 'mydatabaseuser',
            'NAME': 'mydatabase',
            'TEST': {
                'NAME': 'mytestdatabase',
            },
        },
    }
    
    0 讨论(0)
  • 2020-12-04 16:04

    This accelerated dramatically test execution.

    import sys
    
    if 'test' in sys.argv:
        DATABASES['default'] = {
            'ENGINE': 'django.db.backends.sqlite3',
            'TEST_CHARSET': 'UTF8', # if your normal db is utf8
            'NAME': ':memory:', # in memory
            'TEST_NAME': ':memory:', # in memory
        }
    
        DEBUG = False # might accelerate a bit
        TEMPLATE_DEBUG = False
    
        from django.core.management import call_command
        call_command('syncdb', migrate=True) # tables don't get created automatically for me
    
    0 讨论(0)
提交回复
热议问题