OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Unable to mock a function in Python (Django)

  • Thread starter Thread starter steinarey
  • Start date Start date
S

steinarey

Guest
I'm trying to mock a function used within the function I'm testing, but for some reason I'm not seeing the original function always runs, not the one I created as a mock.

The code I'm testing has this type of setup:

Code:
def function_being_tested(foo):
    ...
    function_i_want_to_mock(bar)
    ...

def function_i_want_to_mock(bar)
    print("Inside original function")
    ...

I've installed Mock and I've tried using unittest.mock patch

Currently the test file uses this setup:

Code:
import mock
from django.test import TestCase


def mock_function_i_want_to_mock(bar):
    print(bar)
    return True


class SupportFunctionsTestCases(TestCase):

    @mock.patch("path.to.function.function_i_want_to_mock", mock_function_i_want_to_mock)
    def test_function_being_tested(self):
        # setup
        result = function_being_tested(test_foo)
        self.assertEqual(result, expected_result)

What then happens is when I run the test, I always get: "Inside original function", not the parameter printed so it's always running the original function.

I've used this exact setup before and it has worked so I'm not sure what's causing this. Probably some wrong setup...

If anyone has a different way of doing this or spots some error, it would be appreciated.
<p>I'm trying to mock a function used within the function I'm testing, but for some reason I'm not seeing the original function always runs, not the one I created as a mock.</p>

<p>The code I'm testing has this type of setup:</p>

<pre><code>def function_being_tested(foo):
...
function_i_want_to_mock(bar)
...

def function_i_want_to_mock(bar)
print("Inside original function")
...
</code></pre>

<p>I've installed Mock and I've tried using unittest.mock patch</p>

<p>Currently the test file uses this setup:</p>

<pre><code>import mock
from django.test import TestCase


def mock_function_i_want_to_mock(bar):
print(bar)
return True


class SupportFunctionsTestCases(TestCase):

@mock.patch("path.to.function.function_i_want_to_mock", mock_function_i_want_to_mock)
def test_function_being_tested(self):
# setup
result = function_being_tested(test_foo)
self.assertEqual(result, expected_result)
</code></pre>

<p>What then happens is when I run the test, I always get: "Inside original function", not the parameter printed so it's always running the original function.</p>

<p>I've used this exact setup before and it has worked so I'm not sure what's causing this. Probably some wrong setup...</p>

<p>If anyone has a different way of doing this or spots some error, it would be appreciated. </p>
 

Latest posts

Top