0

i have a function like this, stored in file example.py:

def my_func(file):
    conn = get_connection()
    conn.upload_file(file)
    conn.execute_file(file)

Now, i want to test it, so i`m using MagicMock in test_example.py like:

@mock.patch("example.get_connection")
def test_my_func(mock_conn):
    mock_conn = MagickMock()
    mock_conn.upload_file = MagickMock(return_value=True)
    
    result= my_func("file.zip")
    mock_conn.upload_file.assert_called()

Now, the test fails with error:

        if self.call_count == 0:
            msg = ("Expected '%s' to have been called." %
                   (self._mock_name or 'mock'))
>           raise AssertionError(msg)
E           AssertionError: Expected 'upload_file' to have been called.

When i debug the test, in example.py, MagickMocks attribute called` is set to True after the call, but back in test_example.py it is False, and the test fails. What am i doing wrong?

Ni3dzwi3dz
  • 177
  • 8
  • 1
    in the line `mock_conn = MagickMock()` you have replaced the reference to the mocked function which was passed in via patch decorator `test_my_func(mock_conn)` with a new mock instance that doesn't get called... try just deleting that line – Anentropic Aug 22 '23 at 13:31

0 Answers0