Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection for startup in test client #4183

Closed
9 tasks done
scottherlihy opened this issue Nov 16, 2021 · 4 comments
Closed
9 tasks done

Dependency Injection for startup in test client #4183

scottherlihy opened this issue Nov 16, 2021 · 4 comments
Labels
question Question or problem question-migrate

Comments

@scottherlihy
Copy link

scottherlihy commented Nov 16, 2021

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# currently:

@app.on_event("startup")
async def startup():
    load_defaults()
    await DB.start_db(settings=settings)
    return

# what I'd like
@app.on_event("startup")
async def startup(defaults_retriever=None):
    if not defaults_retriever:
        defaults_retriever = RealRetriever()
    load_defaults(defaults_retriever)
    await DB.start_db(settings=settings)
    return



# and then in tests I wouldnt need to mock the remote because I can do something like 
client = TestClient(app, startup_args=[MockDefaultsRetriever])

Description

Is it possible to pass an argument to the startup function of the TestClient?

https://fastapi.tiangolo.com/advanced/testing-events/

The docs suggest using TestClient to ensure the startup function gets invoked, but what if the startup function has an argument that is a class that hits a remote and Id like to mock it. Since patching module can be finicky dependency injection is a clean way to accomplish the goal of mocking an object and if something like I suggested in the example code was implemented it would make this very easy.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.70.0

Python Version

Python 3.9.7

Additional Context

No response

@scottherlihy scottherlihy added the question Question or problem label Nov 16, 2021
@DavidBord
Copy link

I think that if that would have been made possible then all the startup & shutdown events would have gotten these args. I wouldn't have used this feature if it was available.
Would you consider using partial from functools for your use case?

@a-n-d-i
Copy link

a-n-d-i commented Dec 24, 2021

Hi, maybe just mock/patch RealRetriever? Or call the decorator directly? I mean you're already passing an Argument to Testclient, namely app. The startup stuff is just buried in there. So doing such an Argument list would be like passing the same stuff twice to avoid changing the insides of the app parameter.

@adriangb
Copy link
Contributor

I think this duplicates the feature request in #617

@scottherlihy
Copy link
Author

I agree, @adriangb. Closing.

@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6443 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

5 participants