[BUG] Unit test gives conflicting result from interacting with docs
See original GitHub issueDescribe the bug
We’re developing an application with docker and after building, I can interact with the docs and see that the endpoints behave as expected… but when running unit tests that emulate the same action, I get an incorrect result
To Reproduce
- Application file:
from fastapi import FastAPI
app = FastAPI()
@router.get("extract/{extraction_id}", response_model=schema.Extraction, status_code=HTTPStatus.OK)
async def get_extraction_by_id(extraction_id: str, {other deps like db, etc}):
try:
extraction: Extraction = _query_extraction(db, extraction_id)
# for recreating the error, the below may work simpler
# raise SQLAlchemyError('some txt')
except SQLAlchemyError as sqla_err:
raise HTTPException(detail=str(sqla_err), status_code=HTTPStatus.NOT_FOUND)
- Testing file
def test_bad_id():
with TestClient(app) as client:
attempt = client.get('extract/1')
assert attempt.status_code == HTTPStatus.NOT_FOUND
- Build the project with docker
- Open the browser and call the endpoint
/extract/1
.
Sending with an extraction_id of ‘1’ causes a validation error within SQLAlchemy because it’s not a valid uuid, so the call to _query_extraction(…) throws an SQLAlchemyError (which is caught and an HTTPError thrown)
- It returns a HTTPError with status code 404
- Run the testing file with pytest
- It fails
===================================== FAILURES ======================================
_____________________________________test_bad_id_____________________________________
def test_bad_id():
with TestClient(app) as client:
attempt = client.get('extract/1', headers=TESTER_10_HEADERS)
> assert attempt.status_code == HTTPStatus.NOT_FOUND
E assert <HTTPStatus.OK: 200> == <HTTPStatus.NOT_FOUND: 404>
E + where <HTTPStatus.OK: 200> = <Response [HTTPStatus.OK]>.status_code
E + and <HTTPStatus.NOT_FOUND: 404> = HTTPStatus.NOT_FOUND
tests/test_endpoint_db.py:115: AssertionError
Expected behavior
Expected the test to pass with receiving the status code of 404
Screenshots
Environment
- osX Catalina locally (Docker I believe alpine python)
- ‘fastapi==0.47.1’
- Python 3.6.8
Additional context
Other tests where we check response codes about validation all pass. This originated after adding an exception handler and noticing the same thing was happening with it… the interactive docs showed that the exception was caught by the handler and the expected response code was received, but when unit testing it was still giving a 200 OK
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
I believe I’ve found the problem, unrelated to FastAPI
We use Mock to build a fake database to query, and it seems that any tests run after the Mock database has been created will forgo the endpoint logic and just jump straight to querying the database (this was implemented by another team so it took me a while to find it)
Moving the tests for invalid id & valid but with empty db to above Mock database creation produced expected results
Closing the issue for now, thank you for your time!
Thanks for the help here everyone! 👏 🙇
Thanks for reporting back and closing the issue 👍