Understanding Unittest.Mock

Using unittest.mock

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

We recreate the same testing double that we created manually with the library, we show how easier it is to do it and how few code it is needed now. We see another example where we also need to specify magic methods and show how we can use MagicMock for it.

Keywords

  • recreate
  • testing
  • double
  • library
  • code
  • example
  • specify
  • magicmock

About this video

Author(s)
Mario Corchero
First online
25 December 2018
DOI
https://doi.org/10.1007/978-1-4842-4413-5_2
Online ISBN
978-1-4842-4413-5
Publisher
Apress
Copyright information
© Mario Corchero 2019

Video Transcript

In this segment, we’re going to see how to use the mock class within unittest.mock to create testing doubles by using their attributes, return_value and side_effect. In the previous segment, we saw how could we, uh, create testing doubles to enhance our experience when we’re testing. And we saw that there was a lot of boilerplate that need to be created as we were basically creating Python class. Um, but here, we’re going to see how can we use unittest.mock Mock class to create those much easily. So here we can see that, you know, we could just create a mock and this works. But we wanted something more interesting. And the constructed mock allow us to pass, uh, two arguments or the attributes that it will have. So for example we can say we can have 2 b And an attribute a and an attribute b. Now when we have this sample_mock. Uh, so first what we do sample_mock.a, this is going to have the value of 1. And sample_mock.b is going to have the value of 2. We could now then use these objects to just pass it on various functions as we need it. And I said the mock class takes those two arguments and they are some specific, uh, arguments that have specific special meaning. This is if we want to create a mock instance that, uh, is a call, we can do for example, sample_mock = Mock and then we can… we can tell him what to return. So for example, we can say return_value = 5. And now every single time we call this mock is going to return 5. So sample_mock, we call it and it’s going to give a 5. And it doesn’t matter the arguments, we passed through it. So we can pass, um, random_values. It doesn’t matter what pass. The mock, whatever, whenever you call it, whatever you call it, is always going to return the value that we have. If we wanted this to be something more dynamic, we can use, uh, a key… uh, another keyword argument called side_effect. So when they can do, um, sample_mock, again, equals mock and then we say side_effect. Now side_effect, takes an iterable and it’s just going to be returning whatever is in the iterable Argument we passed. So if we pass, for example, range 3, which is going to give us, uh, uh, numbers up to three, we’re going to see that when we call sample_mock, again, it doesn’t matter the way we call it, we’re going to get 0, 1, 2, and now once the iterator is exhausted, it’s going to raise an exception. It’s going to raise stop iteration. So basically, side, uh, side_effect is just going to take an iterable and it’s going to return the values one by one. Something that’s really useful as well is that side_effect can be used as simulators. Let’s say that we want to simulate that, uh, when we’re trying to reach to an endpoint of by http we want to… we want to simulate that an error occurred in the connection, right? So we can do this kind of, uh, things with… by using side_effect because with… when we do a sample_mock = Mock and we pass an exception in side_effect, let’s say ValueError, for example, what is going to happen is that when we call that mock, we’re going to get always that, um, that exception raised. So we call it, it’s going to raise that exception. Note this is… this is different from a mistake many people do, which is, calling return_value = ValueError because if you do that, when you call it, what was going to happen is that this is just going to return this exception. This won’t raise it which is what we… you really wanted to do. And, um, last in the list is that if you want for simulation to simulate some kind of, for example, resource that once you have asked for three times then it’s exhausted or something like that, you can also have, uh, side_effect, uh, combining both. So you can do side_effect equals returning some values 1, 3, almost there, and then an exception. So now when we call that, we can see it’s going to return 1, uh, 3, almost there, and then the next time we call it, it’s just going to return. It’s just going to raise that… this value error. So as you can see, they can be mixed, uh, both the exceptions and the values they want to return. And side_effect is the keyword that allow us to dynamically tell Mock what it should return whenever it’s called. And now this goes, uh, as far as you need it because if you wanted to, to say what side_effect is going to do by specifying your own function, you can do this. So for example we can say, um, side_effect whenever it’s called, it’s just going to call print. So now whenever we call this thing, all this going to do is forward whatever you… whatever you ask it to print. So here we can say, if we got sample_mock of 1, uh, what’s going to happen is the side_effect of just having a called print. The same way to print, he can have, your own function, um, say fake function and this can… you can say this takes, uh, one argument. It takes one argument and then we can do here whatever we want to. For example, if you want, just return a times 5 then you could just take this function and say the mock, whatever you call it, it’s just going to call this function. At this point, you’re controlling the logic of it and you can just decide what your Mock is going to do whenever it’s called. And with this, um, you should be able to, to create any kind of, uh, testing double as we’re going to see, um, in the next, uh, in the next jupyter notebook okay. So this is all great. But you might be wondering how do we use this in the real life, uh, scenario, right? Then we’re going to see how can we do the get_user_name or we had before this fake session for this fake_response classes now replaced by unittest.mock. So the first thing we want to do is we’re going to create like the replacement for fake session and then we get fake session equals, um, because… well, before all, like, this is the example we use in the previous segment and we can see we can run it, um, this works because basically we are getting these two fake objects. Now if we were to create it manually, we can say fake, uh, fake_response which is the first one, the simplest one we’re going to do, fake_response is just to Mock. And now, uh, fake_response has, uh, an attribute called JSON which is, um, this method we have here. That was going to do is basically it’s going to be a mock that when you call it, as we saw before, return_value of it is going to be that user name. So whenever you call this thing, it’s going to always return the same. This is identical. And then the other part we need is we need to create fake_session which is basically another Mock that has another method called get that is, again, another mock. And whenever you call it is going to return a fake_response. This, we run it and we get, again, the same result. So this fake session, uh, this get_user_name is passing… is getting the fake session which is then calling get which is a Mock that when you call it is going to… is going to get the fake_response which is a Mock that when you call JSON, uh, is going to return you this user name and then it’s extracting this, um, this item from the user name. This is a simpler way to do, uh, to create those, uh, testing doubles and this can be even further simplified as we’ll see in, uh, in the future parts of, uh, of this video.