Correct way to check server response data (pytest)

  • QA Engineer

    Hello! What is the best way to check the values ​​of a dictionary?

    I wrote a test on pytest in which I send a request to the api with a certain set of data and in response I get a dictionary of something like this:

    {'user': '1', 'objects': [{' id ':' 1 ',' event ': [{' type ':' something ',' timestamp ':' 1522991335319 '}]}],' reached ': True}

    The value of the keys user, id, type and reached will have to match the values ​​that I sent in the request, the value of the timestamp key is not interesting to me.

    If you check the value directly through assert, then checking objects with deeper nesting, for example type, does not look very good in the code:

    assert response ["objects"] [0] ["event"] [0] ["type"] == "something"

    How can you check the values ​​of such a response more efficiently?

  • I usually make the expected and actual tuples, and I compare them.

    expected = ('john doe', '1111', 'something', True)
    actual = (
         response ["user"],
         response ["objects"] [0] ["id"],
         response ["objects"] [0] ["event"] [0] ["type"],
         response ["reached"]
    assert expected == actual

    Run pytest with the -v switch, then it will clearly show which elements in the tuples are not equal.

Suggested Topics