Understanding Unittest.Mock

Validating Calls

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 different tools mock provide to verify how it has been interacted with.


  • tools
  • mock
  • verify
  • interaction

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 can we validate the interactions that our mock… our mock had. We’re going to see how to validate that, uh, functions were called with specific arguments. How can we validate part of these arguments and also how to see the arguments that were used when those mock… when those mocks were called. Let’s, you know, the key part of, uh, unittest.mock. And this is being able to validate all the interactions that our function had with our mock. So here we can see we have mock and this mock has been called with, uh, um, first with a single argument then with two arguments and then with multiple precision arguments and one keyword argument. Now let’s say that we want to validate how we’re using… call this function. So we want to say that you have function like we saw before that you see details, want to see if our function actually called as we inspected this thing. So for this, the simpler way to do it is we can do m.assert_called_with and what this is going to do, is going to check the last called that the mock had and validate against that. So for example here, we would say we want to validate that it was m.assert_called_with 4, 5, 6, and the keyword argument True. So this, um, sorry, I have to run this before. And this, uh, this is going to… this is going to pass through, this is true if we had, for example we said that the function should have… should have been called with 4, 5, 7, this is raised on the session there and therefore we will be able to catch this kind of mistake in the test. Okay. So, um, so we have m.assert_called_with which one that works for the last one, we can see that. For example 1 doesn’t work. So it gives us an error, a nice and beautiful error saying it’s like, hey, you said that a mock should have been called with a 1, but the last call was actual, uh, mock with 4, 5, 6 and the keyword argument being true. So, um, we put the right one and this will pass. But let’s say that we actually wanted to validate the first and the second call like let’s that we don’t care. We just want to make sure that, uh, the mock was called at least once in a specific way. And for that, mock has, uh, assert_any_call. And assert_any_call is basically going to do what the name says. It’s just going to check that it was called at least once, uh, with the arguments passed. So that’s the one that we test. And if you were to pass the wrong one, it would just say that, you know, that call was not found. In Python 3.8, um, there’s going to be a nicer or message telling you the actual calls that were present to help you and throws you this kind of issues. But if you are using a version of Python which is, um, is lower than 3.8, you will still see, um, like this message that it’s just telling you that it was not found. But as we saw because, um, I’m going to say 1, if we do a circle and you call 1, it will pass on our test, it will pass happily. It can happen sometimes that for example, we want to make sure, uh, we want to validate that, um, some of the arguments were passed, right, in one of the calls. So let’s say that we don’t… we really are not interested in this last portion of our argument and we are not interested in the keyword argument. We just want to validate that, uh, m was called with 4 and 5 as the first and the second argument. And to do so, we can use an utility within the unittest.mock which is called any. So from, uh, unittest.mock import any and now any is basically, um, is this class that will match against anything. So we do any here and then any here, this will still pass happily. And what’s happening here is we are still validating this call that we saw, but we are ignoring what’s being passed here. The way it works is by any just being a class that implements the equal operator to just return to, uh, no matter what you compare it to. Okay. So this is as a way to assert so the calls between like if we want to do something just based on, uh, whether it was called or not or ignoring all arguments or just number of times that it’s called… was called and we can do that. We try to use that, uh, that mock have. First of all is called. So called will tell you whether a mock was called or not. If you it in a recently created mock just was only created mock, it will give you false because this mock was still going to create this, so we just create a mock and we call called, you’ll see that nothing happens there. And then the other one is m.call_count. So call_count will tell you the number of times that the function was called. With these two, we can just verify whether functions were called without worrying about, um, the parameters that we are passed so we can implement some further logic. Last but absolutely not least, is mock allow us to access, um, the calls that it was called with and which were the arguments that they… it went… that we past to him. So if we do m.call_args, this has recorded for the last call. Uh, the positional arguments and the keyword arguments. So we have args and kwarg… kwargs. Now if we check args, we can see that this is 4, 5, 6 because this was the last call. Now if we change the keyword arguments, we can see this is dictionary with all the keyword arguments that we’re passed to the… to the last call. Now as we… uh, as we saw in this example, this works only for the last call of mock. If we want to get all the calls that we’re passed, mock has yet one more function which is called, uh, m.call_args_list of that will give us a list of all the calls of the mock. So basically it’s… because we know it’s three, we can, uh, we know that this is going to give us list of three items and each of the item are all the calls. So we can see the first one which was just call with 1. The second one which was call with 2 and 3. And then third one which was 4, 5, and 6 and then the keyword argument say true. With this, uh, utilities with a mock, we can now verify any interaction that our function is doing through this mock, which is one of the key features of mock. Because this allow us not only to, you know, feed the parameter to the one in order provide in the right inputs but also allow us to verify how our function is interacting with something that we are passing in.