python - assert JSON response -


i have python api call, , server response coming me json output.

how can assert "status" output 0 example:

def test_case_connection():     req = requests.get_simple(url=server.my_server, params=my_vars)     assert req["status"]="0" 

is i've tried.

the response looking like:

{"status" : 0, ......} 

error got is:

typeerror: 'response' object has no attribute '__getitem__' 

if need check request successful, using request.status_code trick:

def test_case_connection():     req = requests.get_simple(url=server.my_server, params=my_vars)     assert req.status_code == 200 

if want instead check presence pf specific key-value pair in response, need convert response payload json dict:

import json  def test_case_connection():     req = requests.get_simple(url=server.my_server, params=my_vars)     data = json.loads(req.content)     assert data["status"] == "0" 

if using requests library can avoid converting json manually using builtin json decoder.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -