Python, unittest.mock : Getting object & method names from method_calls

In summary: So you can split it.In summary, the conversation discusses extracting names from an array of method calls in Python. The Python documentation provides an example to retrieve names from mock calls, which can also be applied to method calls. The call object has its own parallel inner hierarchy with names that mimic the called hierarchy. The first element of the call object is a string that represents the called hierarchy, and the remaining elements are the respective arguments. The object named "call" is a global object that spans the entire test and can be used to retrieve information about the method calls. When there are multiple mocks and method calls, each call object will be unique and can be split into a string array using the .split(".") method. Further understanding of this functionality
  • #1
Swamp Thing
Insights Author
962
667
TL;DR Summary
Finding object and method names from the elements of some_mock.method_calls when using unittest.mock
Let's say that my_mock.method_calls gives me this array:

Code:
[call.thing.pop(3, 4), call.thing.bob.smash(6, 7), call.thing.jig.slurp(4)]

How can I extract the names 'thing', 'pop', 'bob', 'smash' etc. from this array?
 
Technology news on Phys.org
  • #3
I don't quite understand how this works, but it does. In your linked doc, they give an example to retrieve names from mock_calls. But I tried it for method_calls and it works just as well.

It's rather counterintuitive:
Code:
>>> my_mock.method_calls[1]
call.thing.bob.buzz(6, 7)
>>> my_mock.method_calls[1][0]
'thing.bob.buzz'

Looking at lines 1 and 2, I would have thought that my_mock.method_calls[1][0] would print 6... but no, it evaluates to 'thing.bob.buzz'.

I guess it has to do with the 'tupleness' of the call object, as the doc puts it.

Anyway, it works... Thanks!
 
  • #4
I'd like to understand the philosophy behind this functionality.

At the moment my notion is this:- "When we invoke a method on a (possibly nested) mock object, then the call object acquires its own parallel inner hierarchy in which the names mimic those in the called hierarchy. This hierarchy within call terminates in a method, which again has the same name as the mocked function that was called. But this method within call, when we call it, returns a tuple in which the first element is a string that tells us the called hierarchy. The remaining elements are the respective called arguments"

Is the above correct?

But in that case, what kind of beast is the object named "call"? Is it a global object that spans the entire test?
What if we have two mocks, mock1 and mock2, and we call mock1.thing.pop() and mock2.thing.pop ..?

In that case, mock1.method_calls and mock2.method_calls will both evaluate to call.thing.pop(). Would those two call objects be the same thing or different things?
 
  • #5
Swamp Thing said:
what kind of beast is the object named "call"? Is it a global object that spans the entire test?
What if we have two mocks, mock1 and mock2, and we call mock1.thing.pop() and mock2.thing.mock ..?

In that case, mock1.method_calls and mock2.method_calls will both evaluate to call.thing.pop(). Would those two call objects be the same thing or different things?
These are the sorts of questions that are best answered by experimenting.
 
  • #7
jedishrfu said:
Are they strings like xxx.yyy.zzz where you can split them into a string array via the .split(".") method?
Yes. That is, the first element of call.thing.pop() is a string of that sort.
 

FAQ: Python, unittest.mock : Getting object & method names from method_calls

What is Python's unittest.mock module?

The unittest.mock module in Python is a testing framework that allows developers to create mock objects and test their interactions with code. It provides a powerful and flexible way to create and manage mock objects in unit tests.

How do I get the object and method names from method_calls using unittest.mock?

To get the object and method names from method_calls using unittest.mock, you can use the call_args and call_args_list attributes of the Mock object. These attributes contain information about the arguments and the method that was called, respectively.

Can I use unittest.mock to mock classes and their methods?

Yes, you can use unittest.mock to mock classes and their methods. This is useful for testing code that interacts with external dependencies, such as databases or APIs. You can use the patch decorator or context manager to mock the class and its methods.

How do I assert that a specific method was called using unittest.mock?

To assert that a specific method was called using unittest.mock, you can use the assert_called or assert_called_with methods of the Mock object. These methods check if the method was called at least once or with specific arguments, respectively.

Is unittest.mock only used for unit testing?

No, unittest.mock is not only used for unit testing. While it is commonly used for testing individual units of code, it can also be used for integration testing and even for creating mock objects in production code. It is a versatile and powerful tool for managing dependencies in your code.

Similar threads

Replies
10
Views
2K
Replies
4
Views
1K
Replies
1
Views
1K
Replies
1
Views
801
Replies
4
Views
4K
Replies
5
Views
3K
Replies
3
Views
693
Replies
1
Views
859
Back
Top