Search code examples
pythonjsonpytest

How to fail a test for an api call if json brackets are not empty


I have a Pytest script making a HTTP call:

def test_bid_submission_send():
# Act:
response = post_requests(token, '/xxx/api/xxx/bidsubmissions/send',
                         setpayload_bidsubmissions_send(powerplantId))
# Assert:
assert not response.json()["error"]
assert response.status_code == 200  # Validation of status code

The response is

{"success":[],"error":[{"hpsId":10037,"powerPlant":{"name":"Saurdal","id":16606,"regionId":20,"priceArea":2,"timeSeries":null,"units":[]}}]}

I want to fail the test if the

"error":[]

has content (which it has in my example above)

If the

"success":[]

has content I would like to pass the test


Solution

  • I want to fail the test if the

    "error":[...]

    j = response.json()
    assert len(j["error"]) == 0
    

    if the

    "success":[] has content I would like to pass the test

    assert len(j["success"]) != 0