I have an app/main.py for FastAPI which does: import qdrant_client as QdrantClient ... qdrant_client = QdrantClient(url=...) qdrant_client.create_collection(...) ... app = FastAPI() ... @app.get("/my_endpoint") def handle_my_endpoint(query: str): ... qdrant_client.query_points(...) And I want to write a unit for /my_endpoint. So I have in the unit test file: from fastapi.testclient import TestClient from app.main import app class Test(unittest.TestCase): @patch('app.main.QdrantClient', qdrant_client=MagicMock()) def test_my_endpoint(self, qdrant_client): qdrant_client.create_collection.return_value = None with TestClient(app) as client: resp = client.get("/my_endpoint", params={...}) ... But it doesn't work, it insists on actually using the real client which tries to really call a Qdrant server, which fails... What am I missing to set up that patch correctly? Continue reading...