Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Side Effects: Advanced Test (Part 1)

00:00 Okay, so you’ve seen how you can assign an exception to the .side_effect of a Mock object. Now, let’s see how we can get a little more dynamic and assign functions as side effects to Mock objects.

00:18 Let’s pretend that we want to mock a request to this get_holidays() function, which uses this /holidays API, and we want to log the request as well.

00:31 Something very commonly done in web servers and clients is to generate log statements so you can view the activity of your API. So let’s go into our TestGetHolidays class and we’ll write another test.

00:45 Let’s call it def log_request(), and we’ll have to pass it self and a url. And rather than importing the logging module, let’s just go ahead and use print() to say f'Making a request to {url}' and we’ll make this an f-string.

01:09 We’re going to make a request to this url endpoint and let’s use Mock to do that, so we’ll mock a request to our /holidays API.

01:19 Since we don’t have this running, it’s not an actual API on my machine; we’re going to imitate its functionality using mocks. So in this .log_request() function, we’re going to create a response_mock, and this’ll be a Mock object—a very flexible object.

01:38 And what do we want to mock? What actually comes from a response? Let’s look at our function.

01:46 So, we use request.get(), we’re already mocking requests, and we have a .status_code that we check, and we have this .json() method that we return if the .status_code is 200.

02:01 If you haven’t used requests, when you make a request to a URL, r here—the response from the request—contains a method called .json(), and it returns the response as a Python dictionary.

02:16 So we want to mock this response, and we also want to mock this .status_code attribute from the request.

02:25 So with that in mind, let’s go back down into our .log_request() function, and we’ll add on to this response_mock. We need to mock that .status_code, so we’ll say response_mock.status_code, and let’s make it a successful response, so we’ll set that to 200.

02:46 And then the second thing we want to mock is that .json() method. The .json() method will return a dictionary. The keyword there is return, so we want to set this .return_value to a dictionary.

03:01 And this is where we can have fun and set the response, whatever we want. This is get_holidays(), so let’s come up with a couple holidays. Let’s say 25th of December is 'Christmas' and January 1st is 'New Years'.

03:23 We have created this response_mock with a .status_code of 200, and it’s returning this dictionary. This is going to be the .json() method’s return value.

Become a Member to join the conversation.