-
First check
ExampleThis is the code for my from fastapi import FastAPI
from fastapi.testclient import TestClient
from fastapi.exceptions import HTTPException
app = FastAPI()
@app.get("/")
async def read_main():
return do_hello()
def do_hello():
return {"msg": "Hello World"}
### Test
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
def test_failure(mocker):
mocked_get = mocker.patch("main.test_main.do_hello")
mocked_get.side_effect = Exception("Something broke")
# If using the below exception instead, it doesn't break and the assertion gets executed.
# mocked_get.side_effect = HTTPException(500)
response = client.get("/")
assert response.status_code == 500And I run it with DescriptionI want to test a custom error handler. To do this, I mock a function that is called by my endpoint, and I expect to be able to assert that I get a 500 response with the description of the problem formatted the way I like. If the exception thrown is other than an Environment
Additional context |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 1 reply
-
|
When I extend your snippet with an exception handler, it works for me except for the bare Examplefrom fastapi import FastAPI
from fastapi.testclient import TestClient
from starlette.requests import Request
from starlette.responses import Response
app = FastAPI()
class MyError(Exception):
...
@app.exception_handler(MyError)
def exception(request: Request, exc: Exception):
print("On the application:")
print(exc)
print("cause: ", exc.__cause__)
return Response(status_code=500)
@app.get("/")
async def read_main():
return do_hello()
def do_hello():
return {"msg": "Hello World"}
### Test
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
def test_failure(mocker):
mocked_get = mocker.patch("app.two.do_hello")
# mocked_get.side_effect = MyError("Something broke")
# If using the below exception instead, it doesn't break and the assertion gets executed.
mocked_get.side_effect = KeyError()
response = client.get("/")
assert response.status_code == 500Is this what you were looking for? |
Beta Was this translation helpful? Give feedback.
-
|
That's exactly the problem I'm describing: it works for me with any exception but the bare The handler for the bare So, from what I'm reading, there is no way to test a custom handler for |
Beta Was this translation helpful? Give feedback.
-
|
Looking at the linked issue, I think you might be correct about the fact that There is a small difference between our snippets, though. |
Beta Was this translation helpful? Give feedback.
-
|
Had any headway with this @oji? |
Beta Was this translation helpful? Give feedback.
-
|
@chbndrhnns your snippet doesn't solve the problem when you want to test how your app behaves if, for example, an unexpected error (i.e. IOError) happens. See the linked issue. |
Beta Was this translation helpful? Give feedback.
-
|
One thing which worked for me was to add the argument I'm using fastapi 0.68.1 which installs startlette 0.14.2, so I do not know if this option is available for earlier version... So the whole snippet is |
Beta Was this translation helpful? Give feedback.
-
|
I had the same issue where the exceptions were being raised when trying to do the request on the client and i realized, the exceptions handlers weren't defined in the correct place, my main.py was something like: def create_app():
app = FastAPI()
...
return app
@app.exception_handler(MyException)
def handle_my_exception(....):
passmy conftest.py ìs: app = create_app()
client = TestClient(app)The exceptions handlers weren't being defined on the testing app instance. def create_app():
app = FastAPI()
...
@app.exception_handler(MyException)
def handle_my_exception(....):
pass
return appHope it helps somebody googling this error. |
Beta Was this translation helpful? Give feedback.
-
|
With #9007 (comment) |
Beta Was this translation helpful? Give feedback.
One thing which worked for me was to add the argument
raise_server_exceptionswhich is by default set toTruein the test client:I'm using fastapi 0.68.1 which installs startlette 0.14.2, so I do not know if this option is available for earlier version...
So the whole snippet is