Understanding Unittest.Mock

Creating Spies

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment focus on the usage of the wraps keyword, a not well known feature of mocks that allow inspecting interactions with real objects.


  • usage
  • wraps keyword
  • mock
  • interaction
  • real objects

About this video

Mario Corchero
First online
25 December 2018
Online ISBN
Copyright information
© Mario Corchero 2019

Video Transcript

In this segment, we’re going to see how to create spies with mocks through wraps. It will allow us to see in the interactions that our system has without having to fake the original object. Let’s see, you know, more interesting situations. We come back to the get_user_name example and we are going to see how can we, uh, validate the interaction that our function has with the… with this session. We felt the need of actually provide the name. So this is basically the typical situation where we may have, uh, an integration test that’s still going outside. And we… but we want to validate our functions interacting with it. So we still have this get_user function that takes, uh, requests.session so that you can here see the requests.session. And we have… we call this get_user_name that basically returns our name. So what if we want to make sure that get_user_name, it’s calling get on this session, right? How can we do this? And the option may be how we saw before to create fake, um, session. But we might not want to create, to isolate our test from the… from the environment because on this situation, we want to create integration test and we are not worried about that. So if we wanted to do that, we can create what’s known that’s, uh, as a spy, you know, testing domain by the testing gurus And what the spy does is basically it’s an object that records all interactions but still… but still for what’s… for what’s… or it’s behavior to an original object. So we know that with mock, we could, uh, we could have, um, we could have… we could validate, uh, the interaction and we’ve seen the side effect that might be a way, um, to do it. But there… there’s a much better way to do it and this is by using wraps, the keyword wraps. So, uh, what we’re going to do here is we do from unittest.mock import our swiss army, uh, knife which is a Mock. And now we’re going to create a spy which is just going to be mock and then wraps and we take the original session. Okay. So now this, um, sorry, session. So now this spy object is the same as, uh, this session but it won’t record everything in it. What this means is that if now we do get_user_name but instead of session, we do spy. We’re going to get the same result, but spy has recorded everything that happened within it. So now we can do the spy., um, we see that session is passed here and it was okay get. So we do spy.get, this is, uh, this is still a mock and this has recorded everything. So spy.get is still a mock and we can ask what’s spy.get.called. Yes. We can do spy.get.call_args for example and we’re going to take… we’re going to have here what’s the input that it was fast. This is extremely useful not only for testing but sometimes we get the banging on trying to understand how something is working to see what’s, you know, what’s in the guts or the function, right? You can just… you can just, um, pass spy rather than the real object then you’ll see how it’s being called. I think it’s really, really powerful. The same way, we can also just ascertain it so we could do spy.get.assert_called_with this thing. And this will pass happily, right. This is a great way to validate how our function inspects… sorry, interacts with, um, external resources or anything we might be interested on. Without we having to mock it because we may not be interested because this may not be a unittest. And I said, this is a really handy debugging tool, um, if you’re having a problem and you don’t know what’s actually happening with the option within it.