Testing custom error handlers #6248
-
First Check
Commit to Help
Example Codeclass BaseError(Exception):
def __init__(self, message, status_code) -> None:
self.message = message
self.status_code = status_code
async def error_handler(request: Request, exc: BaseError):
return JSONResponse(content={"message": exc.message}, status_code=exc.status_code) DescriptionGiven the example code, which is only a custom exception class and its handler, what I was trying while making the tests was to cover the handler lines but it keeps being uncovered, even when I force the exception to be raised. By the time, it's not that relevant because it's only one line but it'd get a pain if one handler is larger... Operating SystemWindows Operating System DetailsNo response FastAPI Version0.78.0 Python Version3.9.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
What tool are you using for testing the application? Also, can you show how are you testing it? |
Beta Was this translation helpful? Give feedback.
-
Btw, this code is similar to yours from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
class BaseError(Exception):
def __init__(self, message: str, status_code: int) -> None:
self.message = message
self.status_code = status_code
app = FastAPI()
@app.exception_handler(BaseError)
async def error_handler(request: Request, exc: BaseError):
print("Hello")
return JSONResponse(content={"message": exc.message}, status_code=exc.status_code)
@app.get("/")
def index():
raise BaseError(message="Error", status_code=404) The output I get when visiting Hello
INFO: 127.0.0.1:63867 - "GET / HTTP/1.1" 404 Not Found Maybe you're missing the |
Beta Was this translation helpful? Give feedback.
-
Thanks for replying! I think I'm not missing the decorator, actually the example code is just a snipper, the actual code is divided into different files, and the decorator is for the app. When I run the app it works and handles the exception properly but my problem is when I want to write the unit tests. Please tell me if I was clear. |
Beta Was this translation helpful? Give feedback.
-
Understood. How are you writing the test and what is the behavior that you expect from the |
Beta Was this translation helpful? Give feedback.
-
I'm running the tests with pytest and I'm patching some random app function with a BaseError side effect so it triggers the handler. What I expect is for the test to execute the handler code lines. FYI I also tried to call the handler method directly but it returns me a coroutine object instead of the JSONResponse. |
Beta Was this translation helpful? Give feedback.
-
I don’t think this works the way you think it works. Especially if you say that when testing the handler directly, and you didn’t expect a coroutine to be returned by an asynchronous function🤔 your (async) test client needs to await the function for it to return a value. Provide the testcase that you tried, including any fixtures and mocking, maybe that clarifies. |
Beta Was this translation helpful? Give feedback.
-
After several tries, I could achieve to cover the file with tests.
Thank you guys for replying! |
Beta Was this translation helpful? Give feedback.
-
The best way to test custom exception handlers would be to test an endpoint that triggers that exception, and verify that the response matches what the custom exception handler is doing. |
Beta Was this translation helpful? Give feedback.
-
Hello, would you have a full working example, please? I would like to catch any exception, that will normally turn into HTTPException with status_code 500. But during testing, the original exception is raised and not handled.
Test file:
|
Beta Was this translation helpful? Give feedback.
-
@Kludex have there been any updates on this issue? I also was not able to directly test the behaviour of the exception handler, it works when running the app and an endpoint raises an unexpected error, but testing it just raises the initial error without going through the handler. My code is similar to @blenzi 's |
Beta Was this translation helpful? Give feedback.
The best way to test custom exception handlers would be to test an endpoint that triggers that exception, and verify that the response matches what the custom exception handler is doing.