Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock self in python?

Consider the following code. I want to mock self.get_value, which is invoked in foo.verify_client()

import unittest
import mock

def mock_get_value(self, value):
    return 'client'

class Foo:
    def __init__(self):
        pass
    def get_value(self, value):
        return value
    def verify_client(self):
        client = self.get_value('client')
        return client == 'client'

class testFoo(unittest.TestCase):
    @mock.patch('self.get_value', side_effect = mock_get_value, autospec = True)
    def test_verify_client(self):
        foo = Foo()
        result = foo.verify_client()
        self.assertTrue(result)

if __name__ == "__main__":
    unittest.main()

But I failed, the errors are as follows.

E
======================================================================
ERROR: test_verify_client (__main__.testFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched
    arg = patching.__enter__()
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1353, in __enter__
    self.target = self.getter()
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1523, in <lambda>
    getter = lambda: _importer(target)
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1206, in _importer
    thing = __import__(import_path)
ImportError: No module named self

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

How can I do it?

like image 764
Searene Avatar asked Sep 26 '16 08:09

Searene


People also ask

How do you mock yourself object in Python?

How do we mock in Python? Mocking in Python is done by using patch to hijack an API function or object creation call. When patch intercepts a call, it returns a MagicMock object by default. By setting properties on the MagicMock object, you can mock the API call to return any value you want or raise an Exception .

Can we mock a variable in Python?

Mock Module Level/Global VariablesWith a module variable you can can either set the value directly or use mock. patch . In the following example we have the variable client_id which is a global variable inside the app.

How do you mock a class in Python?

To mock a method in a class with @patch. object but return a different value each time it is called, use side_effect. Side effect allows you to define a custom method and have that method called each time your mock method is called. The value returned from this method will be used as the return value your mock method.


1 Answers

I figured it out. Changing this line

@mock.patch('self.get_value', side_effect = mock_get_value, autospec = True)

to

@mock.patch('test.Foo.get_value', mock_get_value)

worked.

Note that the format of the patched function should be module_name + class_name + method_name

like image 176
Searene Avatar answered Oct 19 '22 13:10

Searene