4

I am writing an app with Docker, Flask and Postgres. I currently have one docker-compose.yml file for development and one for testing. The main objective is to use different databases.

I could also have one docker compose file with 2 databases, one service "backend" connected to one db and one "test" connected to the other db...

Is there any recommended way to manage pytest tests in a dockerized flask + postgres app?

my app factory

def create_app(testing=False):
    app = Flask(__name__)
    app.config.from_pyfile("config.py")

    if testing:
        app.config["TESTING"] = True

    initialize_extensions(app)
    app.register_blueprint(phaunos_api)
    register_cli(app)
    return app

basic test module

import pytest
from phaunos import create_app
from phaunos.models import db
from phaunos.phaunos.models import TagType


@pytest.fixture(scope='module')
def test_app():
    app = create_app(testing=True)
    with app.app_context():
        yield app


@pytest.fixture(scope='module')
def test_db(test_app):
    db.drop_all()
    db.create_all()
    yield db
    db.session.remove()
    db.drop_all()


def test_add_tagtype(test_db):
    tt = TagType()
    tt.name = "tagtype1"
    db.session.add(tt)
    db.session.commit()
    assert len(TagType.query.all()) == 1

docker-compose.dev.yml

version: '3'
services:

  db:
    image: postgres
    env_file:
      - .db.dev.env
    volumes:
      - ./data-dev:/var/lib/postgresql/data
    networks:
      - db_nw
  backend:
    env_file:
      - .backend.dev.env
      - .db.dev.env
    build:
      context: backend
      dockerfile: Dockerfile
    volumes:
      - ./backend:/app
    depends_on:
      - db
    networks:
      - db_nw
    ports:
      - "5000:5000"
    entrypoint:
      - backend/boot.dev.sh

networks:
  db_nw:
    driver: bridge

docker-compose.test.yml

version: '3'
services:

  db:
    image: postgres
    env_file:
      - .db.test.env
    networks:
      - db_nw
  backend:
    env_file:
      - .backend.test.env
      - .db.test.env
    build:
      context: backend
      dockerfile: Dockerfile
    depends_on:
      - db
    networks:
      - db_nw
    ports:
      - "5000:5000"
    entrypoint:
      - ./boot.test.sh

networks:
  db_nw:
    driver: bridge

db.dev.env

POSTGRES_USER=some_user
POSTGRES_PASSWORD=some_pwd
POSTGRES_DB=db_dev
POSTGRES_SERVER=db

db.test.env

POSTGRES_USER=some_user
POSTGRES_PASSWORD=some_pwd
POSTGRES_DB=db_test
POSTGRES_SERVER=db

boot.dev.sh

#!/bin/sh
flask db init
flask db migrate
flask db upgrade
exec flask run --host=0.0.0.0

boot.test.sh

#!/bin/sh
flask db init
flask db migrate
flask db upgrade
exec pytest
jul
  • 36,404
  • 64
  • 191
  • 318
  • 1
    This is an old question, but I'm trying to set up something similar. Is there any info you'd like to share about what you went with ultimately or any thoughts you had along the way? One consideration I'm having is that I think I'd like my CI server to be able to have multiple one postgres container per pytest container, and I'm thinking about using https://pypi.org/project/pytest-docker-db/ ... – sinback Jun 17 '21 at 16:37

0 Answers0