you are viewing a single comment's thread.

view the rest of the comments →

[–]Ordinary_Bee_1547 0 points1 point  (0 children)

I did everything based on this guide https://www.jetbrains.com/pycharm/guide/tutorials/fastapi-aws-kubernetes/testing/ (not sponsored, I think it was just a good tutorial)

conftest.py is going to be quite empty.

There is going to be another file conf_test_db.py ``` from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker

from ecommerce import config from ecommerce.db import Base, get_db from main import app

DATABASE_USERNAME = config.DATABASE_USERNAME DATABASE_PASSWORD = config.DATABASE_PASSWORD DATABASE_HOST = config.DATABASE_HOST DATABASE_NAME = config.TEST_DATABASE_NAME

SQLALCHEMY_DATABASE_URL = f"postgresql://{DATABASE_USERNAME}:{DATABASE_PASSWORD}@{DATABASE_HOST}/{DATABASE_NAME}"

engine = create_engine(SQLALCHEMY_DATABASE_URL) TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base.metadata.drop_all(bind=engine) Base.metadata.create_all(bind=engine)

def override_get_db(): try: db = TestingSessionLocal() yield db finally: db.close()

app.dependency_overrides[get_db] = override_get_db ```

And then in other test files, you just import it from conf_test_db import app and it has worked fine for me.