Python模拟补丁一个嵌套函数

时间:2019-03-13 11:32:47

标签: python testing mocking patch python-unittest

这是我的应用程序结构:

|-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)

运行此测试时,遇到以下错误AttributeError: 'Blueprint' object has no attribute 'helpers'

我正在尝试测试foo2,并且想修补foo1内部称为的foo2。如何使用python模拟来测试此功能?

0 个答案:

没有答案