简体   繁体   中英

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 want to determine 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 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM