29

I'm using Python's mock library. I know how to mock a class instance method by following the documentation:

>>> def some_function():
...     instance = module.Foo()
...     return instance.method()
...
>>> with patch('module.Foo') as mock:
...     instance = mock.return_value
...     instance.method.return_value = 'the result'
...     result = some_function()
...     assert result == 'the result'

However, tried to mock a class instance variable but doesn't work (instance.labels in the following example):

>>> with patch('module.Foo') as mock:
...     instance = mock.return_value
...     instance.method.return_value = 'the result'
...     instance.labels = [1, 1, 2, 2]
...     result = some_function()
...     assert result == 'the result'

Basically I want instance.labels under some_function get the value I want. Any hints?

1 Answer 1

36

This version of some_function() prints mocked labels property:

def some_function():
    instance = module.Foo()
    print instance.labels
    return instance.method()

My module.py:

class Foo(object):

    labels = [5, 6, 7]

    def method(self):
        return 'some'

Patching is the same as yours:

with patch('module.Foo') as mock:
    instance = mock.return_value
    instance.method.return_value = 'the result'
    instance.labels = [1,2,3,4,5]
    result = some_function()
    assert result == 'the result

Full console session:

>>> from mock import patch
>>> import module
>>> 
>>> def some_function():
...     instance = module.Foo()
...     print instance.labels
...     return instance.method()
... 
>>> some_function()
[5, 6, 7]
'some'
>>> 
>>> with patch('module.Foo') as mock:
...     instance = mock.return_value
...     instance.method.return_value = 'the result'
...     instance.labels = [1,2,3,4,5]
...     result = some_function()
...     assert result == 'the result'
...     
... 
[1, 2, 3, 4, 5]
>>>

For me your code is working.

Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work. I got the same result as instance.labels = [1, 1, 2, 2], which is this mocked variable didn't get used by some_function. In the documentation it's mocking out method instead of variable.
Updated my answer. Now I'm lost because your code is working.
In my code, labels only appears after calling some function. And that function is called within the function I want to test. Maybe that's the reason. I end up mock out the initialization of the class so that it returns the mock object with behavior i want.
I want to raise an exception when I'm dule.Foo instance is created. So I tried both mock.side_effect and mock.return_value. Both didn't work. Why?
That's a different question and there is no code we can look at, but I guess this question is what you are looking for stackoverflow.com/questions/28305406/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.