mercredi 13 mars 2019

Python Mock Patch a Nested Function

Here is my app structure:

|-project
  |-app
    |-main
      |- init.py
      |- ...
      |- helpers.py
  |-tests
      |- test_helpers.py

helpers.py:

def foo1():
    return bar

def foo2(arg):
    bar = foo1()
    # do some stuff
    return bar

test_helpers.py:

import unittest
from app.main import helpers
import mock

class TestHelpers(TestCase): 

    @mock.patch('app.main.helpers.foo1')
    def test_foo2(self, mocked_foo1_value):
        mocked_foo1_value.return_value = 'some_value'
        res = helpers.foo2('arg')
        expected = 'some_value2')
        self.assertEqual(res, expected)

When I run this test, I run into the following error AttributeError: 'Blueprint' object has no attribute 'helpers'.

I am trying to test foo2 and I want to patch foo1 which is called inside of foo2. How can I use python mocking to test this function?

Aucun commentaire:

Enregistrer un commentaire