Home > other >  Django didn't create test database
Django didn't create test database

Time:04-06

I have a postgresql database defined in settings.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'blog',
    'USER': 'blog',
    'PASSWORD': 'blog',
    'HOST': 'postgres',
    'PORT': '5432',
}}

Also I have a dummy test that creates a few objects in db

class LogicTestCase(TestCase):
@classmethod
def setUp(cls):
    User.objects.create(username='test_user')
    Tag.objects.create(name='test_tag')
    Post.objects.create(title='test_title', post_text='test post text',
                        author=User.objects.get(username='test_user'))

def test_home(self):
    self.view = Home()
    print(self.view.get_context_data())

But when I run the tests - Django does not create a test database, instead of it Django uses my main DB to create objects. What have I done wrong with configuring my project? Django version==3.2.9

I run tests in a docker container, here is my docker-compose

version: '3'

services:
  blog:
    build: .
    command: bash -c 'python manage.py runserver 0.0.0.0:8000'
    ports:
      - "8000:8000"
    container_name: blog
    volumes:
      - .:/usr/src/blog
    restart: always
    depends_on:
      - postgres

  redis:
    image: redis
    restart: always

  postgres:
    image: postgres
    volumes:
      - '/tmp/postgres:/var/lib/psql/data'
    environment:
      POSTGRES_DB: 'blog'
      POSTGRES_USER: 'blog'
      POSTGRES_PASSWORD: 'blog'
      PGDATA: 'var/lib/psql/data/postgres'


  celery:
    restart: always
    build: .
    container_name: 'blog_celery'
    command: bash -c 'celery -A justblog worker -l INFO -E -B'

CodePudding user response:

From the docs enter image description here

Make sure your test classes subclass django.test.TestCase and not unittest.TestCase.

  • Related