<h3 dir="auto">First Check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
</ul>
<h3 dir="auto">Commit to Help</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I commit to help with one of those options <g-emoji class="g-emoji" alias="point_up_2" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f446.png">👆</g-emoji></li>
</ul>
<h3 dir="auto">Example Code</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# currently:

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

# what I'd like
@app.on_event(&quot;startup&quot;)
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])"><pre class="notranslate"><span class="pl-c"># currently:</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">on_event</span>(<span class="pl-s">"startup"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">startup</span>():
    <span class="pl-en">load_defaults</span>()
    <span class="pl-k">await</span> <span class="pl-v">DB</span>.<span class="pl-en">start_db</span>(<span class="pl-s1">settings</span><span class="pl-c1">=</span><span class="pl-s1">settings</span>)
    <span class="pl-k">return</span>

<span class="pl-c"># what I'd like</span>
<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">on_event</span>(<span class="pl-s">"startup"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">startup</span>(<span class="pl-s1">defaults_retriever</span><span class="pl-c1">=</span><span class="pl-c1">None</span>):
    <span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">defaults_retriever</span>:
        <span class="pl-s1">defaults_retriever</span> <span class="pl-c1">=</span> <span class="pl-v">RealRetriever</span>()
    <span class="pl-en">load_defaults</span>(<span class="pl-s1">defaults_retriever</span>)
    <span class="pl-k">await</span> <span class="pl-v">DB</span>.<span class="pl-en">start_db</span>(<span class="pl-s1">settings</span><span class="pl-c1">=</span><span class="pl-s1">settings</span>)
    <span class="pl-k">return</span>



<span class="pl-c"># and then in tests I wouldnt need to mock the remote because I can do something like </span>
<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-v">TestClient</span>(<span class="pl-s1">app</span>, <span class="pl-s1">startup_args</span><span class="pl-c1">=</span>[<span class="pl-v">MockDefaultsRetriever</span>])</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">Is it possible to pass an argument to the startup function of the TestClient?</p>
<p dir="auto"><a href="https://fastapi.tiangolo.com/advanced/testing-events/" rel="nofollow">https://fastapi.tiangolo.com/advanced/testing-events/</a></p>
<p dir="auto">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.</p>
<h3 dir="auto">Operating System</h3>
<p dir="auto">macOS</p>
<h3 dir="auto">Operating System Details</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">FastAPI Version</h3>
<p dir="auto">0.70.0</p>
<h3 dir="auto">Python Version</h3>
<p dir="auto">Python 3.9.7</p>
<h3 dir="auto">Additional Context</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">First Check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
</ul>
<h3 dir="auto">Commit to Help</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I commit to help with one of those options <g-emoji class="g-emoji" alias="point_up_2" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f446.png">👆</g-emoji></li>
</ul>
<h3 dir="auto">Example Code</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# main.py
from random import randint
from fastapi import FastAPI, WebSocket

app = FastAPI()

@app.get(&quot;/&quot;, tags=[&quot;Test&quot;])
async def index():
    return {
        &quot;response&quot;: &quot;Index&quot;
    }


@app.websocket(&quot;/wsx&quot;)
async def test_socket(websocket: WebSocket, start: int = 1, stop: int = 100):
    await websocket.accept()
    while True:
        try:
            number = await websocket.receive_text()
            rndm = randint(start, stop)
            response = int(number) + rndm
            await websocket.send_json(
                {&quot;number&quot;: response}
            )
        except Exception as error:
            break


# router.py
from random import randint
from fastapi import APIRouter, WebSocket

socket_router = APIRouter(
    prefix=&quot;/wsx&quot;,
    tags=[&quot;Socket&quot;]
)


@socket_router.websocket(&quot;/&quot;)
async def test_socket(websocket: WebSocket, start: int = 1, stop: int = 100):
    await websocket.accept()
    while True:
        try:
            number = await websocket.receive_text()
            rndm = randint(start, stop)
            response = int(number) + rndm
            await websocket.send_json(
                {&quot;number&quot;: response}
            )
        except Exception as error:
            break"><pre class="notranslate"><span class="pl-c"># main.py</span>
<span class="pl-k">from</span> <span class="pl-s1">random</span> <span class="pl-k">import</span> <span class="pl-s1">randint</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>, <span class="pl-v">WebSocket</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/"</span>, <span class="pl-s1">tags</span><span class="pl-c1">=</span>[<span class="pl-s">"Test"</span>])</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">index</span>():
    <span class="pl-k">return</span> {
        <span class="pl-s">"response"</span>: <span class="pl-s">"Index"</span>
    }


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">websocket</span>(<span class="pl-s">"/wsx"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">test_socket</span>(<span class="pl-s1">websocket</span>: <span class="pl-v">WebSocket</span>, <span class="pl-s1">start</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>, <span class="pl-s1">stop</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-c1">100</span>):
    <span class="pl-k">await</span> <span class="pl-s1">websocket</span>.<span class="pl-en">accept</span>()
    <span class="pl-k">while</span> <span class="pl-c1">True</span>:
        <span class="pl-k">try</span>:
            <span class="pl-s1">number</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">websocket</span>.<span class="pl-en">receive_text</span>()
            <span class="pl-s1">rndm</span> <span class="pl-c1">=</span> <span class="pl-en">randint</span>(<span class="pl-s1">start</span>, <span class="pl-s1">stop</span>)
            <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-en">int</span>(<span class="pl-s1">number</span>) <span class="pl-c1">+</span> <span class="pl-s1">rndm</span>
            <span class="pl-k">await</span> <span class="pl-s1">websocket</span>.<span class="pl-en">send_json</span>(
                {<span class="pl-s">"number"</span>: <span class="pl-s1">response</span>}
            )
        <span class="pl-k">except</span> <span class="pl-v">Exception</span> <span class="pl-k">as</span> <span class="pl-s1">error</span>:
            <span class="pl-k">break</span>


<span class="pl-c"># router.py</span>
<span class="pl-k">from</span> <span class="pl-s1">random</span> <span class="pl-k">import</span> <span class="pl-s1">randint</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">APIRouter</span>, <span class="pl-v">WebSocket</span>

<span class="pl-s1">socket_router</span> <span class="pl-c1">=</span> <span class="pl-v">APIRouter</span>(
    <span class="pl-s1">prefix</span><span class="pl-c1">=</span><span class="pl-s">"/wsx"</span>,
    <span class="pl-s1">tags</span><span class="pl-c1">=</span>[<span class="pl-s">"Socket"</span>]
)


<span class="pl-en">@<span class="pl-s1">socket_router</span>.<span class="pl-en">websocket</span>(<span class="pl-s">"/"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">test_socket</span>(<span class="pl-s1">websocket</span>: <span class="pl-v">WebSocket</span>, <span class="pl-s1">start</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>, <span class="pl-s1">stop</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-c1">100</span>):
    <span class="pl-k">await</span> <span class="pl-s1">websocket</span>.<span class="pl-en">accept</span>()
    <span class="pl-k">while</span> <span class="pl-c1">True</span>:
        <span class="pl-k">try</span>:
            <span class="pl-s1">number</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">websocket</span>.<span class="pl-en">receive_text</span>()
            <span class="pl-s1">rndm</span> <span class="pl-c1">=</span> <span class="pl-en">randint</span>(<span class="pl-s1">start</span>, <span class="pl-s1">stop</span>)
            <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-en">int</span>(<span class="pl-s1">number</span>) <span class="pl-c1">+</span> <span class="pl-s1">rndm</span>
            <span class="pl-k">await</span> <span class="pl-s1">websocket</span>.<span class="pl-en">send_json</span>(
                {<span class="pl-s">"number"</span>: <span class="pl-s1">response</span>}
            )
        <span class="pl-k">except</span> <span class="pl-v">Exception</span> <span class="pl-k">as</span> <span class="pl-s1">error</span>:
            <span class="pl-k">break</span></pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">When I use this WebSocket inside of "mani.py", everything is Ok.<br>
But when I move this WebSocket from "main.py" to "router.py" and I use APIRouter in "router.py" and then I include this router inside of "main.py", WebSocket doesn't work!</p>
<p dir="auto">what's the solution</p>
<h3 dir="auto">Operating System</h3>
<p dir="auto">Windows</p>
<h3 dir="auto">Operating System Details</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">FastAPI Version</h3>
<p dir="auto">0.73.0</p>
<h3 dir="auto">Python Version</h3>
<p dir="auto">3.7.9</p>
<h3 dir="auto">Additional Context</h3>
<p dir="auto"><em>No response</em></p>