question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Unit test gives conflicting result from interacting with docs

See original GitHub issue

Describe 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

  1. 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)
  1. Testing file
def test_bad_id():
    with TestClient(app) as client:

        attempt = client.get('extract/1')

        assert attempt.status_code == HTTPStatus.NOT_FOUND
  1. Build the project with docker
  2. 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
  1. 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

interactive docs unit test

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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mwilson8commented, Apr 29, 2020

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!

0reactions
tiangolocommented, Jun 5, 2020

Thanks for the help here everyone! 👏 🙇

Thanks for reporting back and closing the issue 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices for writing unit tests - .NET - Microsoft Learn
Repeatable: Running a unit test should be consistent with its results, that is, it always returns the same result if you don't change...
Read more >
Unit Testing: Definition, Examples, and Critical Best Practices
Unit testing and results—finally, the unit test runs and developers can identify errors or issues in the code and fix them.
Read more >
Unit Tests and Integration Tests: Key Differences and Similarities
Read the article for an overview of both unit testing and integration testing, including their similarities and differences.
Read more >
Testing best practices - GitLab Docs
GitLab development guidelines - testing best practices. ... This results in them looking very different from the actual application. ... Fast unit tests....
Read more >
Build local unit tests - Android Developers
However, not being able to interact with the Android framework creates ... The following example shows how you might create a unit test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found