Home>

I am writing my first autotests using python3 and pytest. There are some difficulties with writing asserts when the server response in json format contains an array of objects. Specifically, it is impossible to correctly indicate the parameter by which I compare. Here's a sample response:

{'users': [{'displayName': 'Tester Tester',
            'familyName': 'Testers',
            'givenName': 'Tester',
            'id': '48',
            'properties': [{'type': 'user_home',
                            'value': '0ce419ba-deb4-4b92-a573-c6809a581236'},
                           {'type': 'name', 'value': 'Tester'},
                           {'type': 'thumbnailUrl',
                            'value': '5223a9c9-b4b6-49cf-88e1-298183d577bf'},
                           {'type': 'displayName', 'value': 'Tester Tester'},
                           {'type': 'birthdate',
                            'value': '1996-04-09T20: 00: 00.000Z'},
                           {'type': 'thumbnailUrl',
                            'value': '5223a9c9-b4b6-49cf-88e1-298183d577bf'},
                           {'type': 'user_home',
                            'value': '0ce419ba-deb4-4b92-a573-c6809a581236'}],
            'username': 'user_802ff83720d83f9626836f86dd9905e2'},
           {'displayName': 'Anonymous',
            'familyName': None,
            'givenName': 'Anonymous',
            'id': '148',
            'properties': [],
            'username': 'user_60a58d33a3effbcc1bc261e0c69688a1'},
           {'displayName': 'Anonymous',
            'familyName': None,
            'givenName': 'Anonymous',
            'id': '248',
            'properties': [],
            'username': 'user_9fb59ad75b801e0c4671b11db5f66010'}]}

And I need to write an assert on one of the objects in "Properties". Please tell me how you can do this? I tried many options. Here's one example I've tried: assert response_body ['users']== {'id': 148} . Throws an AssertionError.

response_body ['users'] [1] ['id']== '148'

Jack_oS2021-02-23 22:56:09

This will not be a completely correct comparison, as it seems to me. And if our record is not the second in a row? and not the 'id' parameter will I compare?)

Алексей Куклин2021-02-23 22:56:09

correct only for your example, of course; here only in general terms it is shown how to reach the meaning of the dictionary in Python

Jack_oS2021-02-23 22:56:09

I'll explain a little then. There is another test case, where the providers connected to the application are displayed in the list, they can be from 0 to 10, for example. The answer is also given to me by an array of objects, from which I want, for example, to select a specific provider with a specific name, and it can be either the first in the list or the fifth. How then to be in this case?) I thought maybe there is some kind of universal solution, without specifying the ordinal number of the record.

Алексей Куклин2021-02-23 22:56:09

This is not a universal solution, but a magical one -so that it somehow finds the desired entry itself. There is no magic solution. A universal solution is to find a specific element with the desired name (for example, a regular loop through the list), and check its id.

insolor2021-02-23 22:56:09
  • Answer # 1

    If you understand your condition correctly: the code checks that for at least one object from the listresponse_body ['users']condition is satisfieduser ['id']== '148'

    assert any (user ['id']== '148' for user in response_body ['users'])
    

    Approximately corresponds to this code:

    for user in response_body ['users']:
        if user ['id']== '148':
            assert True
            break
    else:
        assert False
    

    Thank you so much!

    Алексей Куклин2021-02-23 22:56:09

    @AlexeyKuklin if the answer helped, you can mark it as accepted (check the box to the left of the answer)

    insolor2021-02-23 22:56:09