0

Here is the proj structure that I have -

src/
    package1/
       __init__.py
       file1.py # has the method -> _method_name
       
    tests/
       __init__.py
       test_file1.py

I am using unittest to test my code. I want to mock a method in a package1 and run my tests. Here is what i am doing to mock that method -

with unittest.mock.patch('package1.file1._method_name') as method_name_mock:

This should create a MagicMock for the method name. When i start debugging, it invokes the real method which i don't want.

I am checking if the mock was called twice which it should but somehow it fails since the real method gets called -

assert method_name_mock.call_count == 2

What am I doing wrong?

Dan
  • 79
  • 10
  • You are probably patching the wrong object - this depends on how you import the method in the real code. Please check [where to patch](https://docs.python.org/3/library/unittest.mock.html#id6). – MrBean Bremen May 21 '21 at 18:08
  • ah correct. Was using patch wrong. Thanks – Dan May 24 '21 at 07:27

0 Answers0