Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking requests.post and requests.json decoder python

I'm creating a test suite for my module that uses the requests library quite a bit. However, I'm trying to mock several different return values for a specific request, and I'm having trouble doing so. Here is my code snippet that doesn't work:

class MyTests(unittest.TestCase):

    @patch('mypackage.mymodule.requests.post') 
    def test_change_nested_dict_function(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json = nested_dictionary
        modified_dict = mymodule.change_nested_dict()
        self.assertEqual(modified_dict['key1']['key2'][0]['key3'], 'replaced_value')

The function I am attempting to mock:

import requests

def change_nested_dict():
    uri = 'http://this_is_the_endpoint/I/am/hitting'
    payload = {'param1': 'foo', 'param2': 'bar'}
    r = requests.post(uri, params=payload)

    # This function checks to make sure the response is giving the 
    # correct status code, hence why I need to mock the status code above
    raise_error_if_bad_status_code(r)

    dict_to_be_changed = r.json()

    def _internal_fxn_to_change_nested_value(dict):
        ''' This goes through the dict and finds the correct key to change the value. 
            This is the actual function I am trying to test above'''
        return changed_dict


    modified_dict = _internal_fxn_to_change_nested_value(dict_to_be_changed)

    return modified_dict

I know a simple way of doing this would be to not have a nested function, but I am only showing you part of the entire function's code. Trust me, the nested function is necessary and I really do not want to change that part of it.

My issue is, I don't understand how to mock requests.post and then set the return value for both the status code and the internal json decoder. I also can't seem to find a way around this issue since I can't seem to patch the internal function either, which also would solve this problem. Does anyone have any suggestions/ideas? Thanks a bunch.

like image 393
the_deuce Avatar asked Jan 24 '18 21:01

the_deuce


People also ask

What is the difference between mock and MagicMock?

So what is the difference between them? MagicMock is a subclass of Mock . It contains all magic methods pre-created and ready to use (e.g. __str__ , __len__ , etc.). Therefore, you should use MagicMock when you need magic methods, and Mock if you don't need them.

What is Python MagicMock?

MagicMock. MagicMock objects provide a simple mocking interface that allows you to set the return value or other behavior of the function or object creation call that you patched. This allows you to fully define the behavior of the call and avoid creating real objects, which can be onerous.


2 Answers

I bumped here and although I agree that possibly using special purpose libraries is a better solution, I ended up doing the following

from mock import patch, Mock

@patch('requests.post')
def test_something_awesome(mocked_post):
    mocked_post.return_value = Mock(status_code=201, json=lambda : {"data": {"id": "test"}})

This worked for me for both getting the status_code and the json() at the receiver end while doing the unit-test.

Wrote it here thinking that someone may find it helpful.

like image 80
SRC Avatar answered Sep 21 '22 09:09

SRC


When you mock a class each child method is set up as a new MagicMock that in turn needs to be configured. So in this case you need to set the return_value for mock_post to bring the child attribute into being, and one to actually return something, i.e:

mock_post.return_value.status_code.return_value = 200
mock_post.return_value.json.return_value = nested_dictionary

You can see this by looking at the type of everything:

print(type(mock_post))
print(type(mock_post.json))

In both cases the type is <class 'unittest.mock.MagicMock'>

like image 24
match Avatar answered Sep 19 '22 09:09

match