Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check the JSON response types when using FastAPI testclient

I’m using the FastAPI testclient to test the response of my API routes. Now I wan’t to determ the types of the JSON response.

For example my response is:

{"userID": 50}

I know how to test this hard coded:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

assert response.json == {'userID': 50}

But my goal is wether to check only if the response returns the key userID or check the type of the key value. For example:

assert response.json == {'userID': int}

Ultimately, I am only looking for a way to check whether the desired key names are present.

Thank you in advance

>Solution :

You can use any regular Python supported way of checking a type:

user_response = response.json()
assert type(user_response['userID']) == int

or

assert isinstance(user_response['userID'], 3)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading