Home > other >  How to check the JSON response types when using FastAPI testclient
How to check the JSON response types when using FastAPI testclient

Time:11-11

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:

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

CodePudding user response:

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)
  • Related