Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
FastAPI endpoint test - database session not sync

I have an issue when I test my API, the DB session at the tests isn't up to date when performing post request.

For example:

@pytest.fixture(scope="module")
def db(engine, tables, users) -> Generator:
    """Returns an sqlalchemy session, and after the test tears down everything properly."""
    try:
        db = TestingSessionLocal()
        yield db
    finally:
        db.close()
def test_test(client: TestClient, db: Session):
    item_id = 1
    params = {
        "item_id": item_id,
    }

    r = client.post("/endpoint",json=params)
    db2: SessionLocal = TestingSessionLocal()  # TEMP SOLUTION
    # item_from_db = service.table.get(db, item_id)  # TODO:  make it work!!
    item_from_db = service.event.get(db2, item_id)
    db2.close()

I see the new item at my DB at run time, but I cant get it with the fixture. Only when I create a new connection to DB, I manage to get the new item I added.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!