mercredi 30 septembre 2015

Python - How to test code which uses an object and its methods using py.test?

I am trying to unit-test the following code with py.test:

def get_service_data():    
    client = requests.session()
    # some task on the client object

    resp = client.get('http://ift.tt/1VmBZCg', params={...})
    temp_json = resp.json()

    result = []
    # lots of processing on temp_json
    return result

However, if I monkeypatch requests.session(), the mocked object will not have the get() method. If I patch requests.session.get(), then the test code will output another error message:

>       monkeypatch.setattr('requests.session.get', lambda: {})
E       Failed: object <function session at 0x1046e3500> has no attribute 'get'

How should I test the above code using py.test?

Aucun commentaire:

Enregistrer un commentaire