Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Attributes

00:00 Once again, here are the methods and attributes associated with the Mock objects. Let’s actually explore a couple more of these. I really like the way that they’re named because they’re kind of self-explanatory, what they do. For example, 'call_args'let’s look at this one. I’m going to delete this print() line and let’s say print(json.dumps.call_args).

00:25 And this one’s actually an attribute, not a method, so we’re just going to print this attribute of this json.dumps Mock object.

00:34 So go down and clear the screen, run the program,

00:39 and we’ll see this call({'a': 1}). So as you may have expected, it prints out the arguments that were called. This can be super useful when you’re running tests and you have a lot of complexity and sometimes the arguments change when you don’t expect them, so this will give you information of what the arguments actually were when the function was called. Another method, another useful tool with Mock objects is we have this .call_count attribute.

01:08 As you can probably guess, when we run this we’ll see 1. This is the number of times the method—or really, the Mock object—has been called.

01:19 And if we call this three times and we run our program again, then we’ll see 3. Another method we can use is called .method_calls.

01:34 So let’s print the return value of this .method_calls method.

01:41 This is an empty list. This one’s a little tricky, in my opinion. It actually works recursively, so if we were using more methods of the json module, this would be useful to show a list of all the methods that we have called.

01:58 And just to kind of clarify that: if we print json.method_calls instead of json.dumps, let’s see how that will change the output.

02:13 Now we can start to see how this is useful. We get this list of call.dumps({'a': 1}) and that repeats three times, and that represents all these calls up here on lines 5 through 7.

02:26 So if we had used, say, another method of the json module, maybe json.loads(), and we passed in some dictionary string, and then let’s run our program again—

02:41 and now we’ll see this list includes this method call as well, this new .loads() call. So .method_calls acts recursively and it shows all the different calls that you used from the, kind of, base—if you will—base Mock object.

02:57 Now you’ve learned some methods you can use to analyze and provide introspection into how your functions are being called, what arguments they’re being called with, how many times they’re being called, and you can use this information to gain insight into your program.

Become a Member to join the conversation.