Search code examples
pythonpython-3.xmockingpython-unittestmagicmock

Cannot Seem to Patch Class & Method In Another File


I've been banging my head against the wall on a small mockup like this:

Here's the tree:

src
├── __init__.py
├── file_a.py
├── file_b.py


test
├── test_a.py

In file_a:

class qaz(object):
    def __init__(self):
        print("\n\nin qaz")

    def exec_bar(self):
        bar_inst = bar()
        bar_inst.execute("a", "b")

In file_b:

class bar(object):
    def __init__(self, a, b):
        print("\n\nin bar")

    def execute(self, c, d):
        print("\n\nin bar -> execute")

So, I want to mock bar so that I can test a without any issues.

In test_a:

from unittest.mock import patch, MagicMock
from src.file_a import qaz
from src.file_b import bar

class BarTester(unittest.TestCase):

    @patch('src.file_b.bar')
    def test_bar(self, mock_bar):
        bar_inst = MagicMock()
        bar_inst.execute.return_value = None
        mock_bar.return_value = bar_inst

        q = qaz()
        q.exec_bar()

This fails every time with something like this:

TypeError: __init__() missing 2 required positional arguments: 'a' and 'b'

This implies the mock isn't working. I can't seem to figure out what I'm getting wrong.


Solution

  • in file_b, you are expecting 2 arguments to be passed in "init"
    def __init__(self, a, b):

    but in file_a when you are creating object for class bar, you are not passing any arguments
    bar_inst = bar()

    which is why are you seeing the error
    TypeError: __init__() missing 2 required positional arguments: 'a' and 'b'

    you do 2 things:

    1. Remove arguments a and b from def __init__(self, a, b):
    2. Pass arguments when

    New Solution:

    from mock import patch
    import unittest
    from src.file_a import qaz
    from src.file_b import bar
    
    class BarTester(unittest.TestCase):
    
        @patch.object(bar, '__init__', return_value=None)
        def test_bar(self, *mock_stdout):
            q = qaz()
            q.exec_bar()