Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the arguments to the new_callable from mock.patch?

I've got the code and the test files:

code.py

class Code:

    def do_something_inside(self, a, b, c):
        return a-b-c

    def do_something(self, b, c):
        self.do_something_inside(30, b, c)

test.py

import unittest
import unittest.mock as mock
from code import Code

class TestStringMethods(unittest.TestCase):
    def setUp(self):
        self.code = Code()

    def do_something_inside_stub(self, a, b, c):
        return a+b+c

    @mock.patch('code.Code.do_something_inside', new_callable=do_something_inside_stub)
    def test_add(self):
        self.assertEquals(self.code.do_something(10, 5), 45)

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

I want to mock the do_something_inside method using do_something_inside_stub, but the execution failed:

E
======================================================================
ERROR: test_add (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/mock.py", line 1171, in patched
    arg = patching.__enter__()
  File "/usr/lib/python3.6/unittest/mock.py", line 1293, in __enter__
    new = Klass(**_kwargs)
TypeError: do_something_inside_stub() missing 4 required positional arguments: 'self', 'a', 'b', and 'c'

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

FAILED (errors=1)

I don't know how to pass the arguments to the new_callable from mock.patch.

like image 585
Dawid Wolski Avatar asked Jun 07 '18 20:06

Dawid Wolski


People also ask

What is the difference between mock and MagicMock?

Mock vs. So what is the difference between them? MagicMock is a subclass of Mock . It contains all magic methods pre-created and ready to use (e.g. __str__ , __len__ , etc.). Therefore, you should use MagicMock when you need magic methods, and Mock if you don't need them.

What is Autospec in mock patch?

When you patch a function using mock, you have the option to specify autospec as True: If you set autospec=True then the mock with be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced.

What is the difference between mock and patch?

Mock is a type, and patch is a context. So you are going to pass or receive Mock instances as parameters, and apply patch contexts to blocks of code. (Lowercase 'mock' is just the name of the package.) Tests and test classes are often decorated with calls to patch.

How does Unittest mock patch work?

unittest. mock provides a powerful mechanism for mocking objects, called patch() , which looks up an object in a given module and replaces that object with a Mock . Usually, you use patch() as a decorator or a context manager to provide a scope in which you will mock the target object.


1 Answers

new_callable requires a class inherited from Mock, whose object will be created for this mock. The functionality you are looking for is side_effect. Ref: https://docs.python.org/3/library/unittest.mock.html#the-mock-class

Use the following code:

@mock.patch('code.Code.do_something_inside', side_effect=do_something_inside_stub)
def test_add(self, do_something_inside_mock):
    do_something_inside_mock.assert_called
like image 92
Aswin Murugesh Avatar answered Oct 26 '22 19:10

Aswin Murugesh