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

  • #1
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?
 

Answers and Replies

  • #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
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
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.
 

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

Replies
8
Views
325
Replies
1
Views
473
Replies
9
Views
616
Replies
10
Views
1K
Replies
3
Views
203
Replies
2
Views
119
Replies
9
Views
1K
Replies
6
Views
550
Replies
6
Views
293
Replies
1
Views
989
Back
Top