[英]patch function with same name as module python django mock
|-- my_module
| |-- __init__.py
| |-- function.py
`-- test.py
在function.py中:
import other_function
def function():
doStuff()
other_function()
return
在__init__.py中
from .function import function
在我的test.py中
from django.test import TestCase
from mock import patch
from my_module import function
class Test(TestCase):
@patch('my_module.function.other_function')
def function_test(self, mock_other_function):
function()
当我运行它时,我得到一个AttributeError:
<@task:项目的my_module.function.function:0x7fed6b4fc198>没有属性'other_function'
这意味着我正在尝试修补函数“function”而不是模块“function”。 我不知道如何理解我想修补模块。
我还想避免重命名我的模块或功能。
有任何想法吗?
[编辑]您可以在https://github.com/vthorey/example_mock运行中找到一个示例
python manage.py test
您可以在__init__.py
以不同的名称使模块可用:
from . import function as function_module
from .function import function
然后你可以在test.py
执行以下操作:
from django.test import TestCase
from mock import patch
from my_module import function
class Test(TestCase):
@patch('my_module.function_module.other_function')
def function_test(self, mock_other_function):
function()
我不认为这是一个特别优雅的解决方案 - 对于一个随意的读者来说代码并不是很清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.