[英]How can I mock an external function within a method in a class
I need some help on mock. 我需要一些关于模拟的帮助。
I have following code in mymodule.py: 我在mymodule.py中有以下代码:
from someModule import external_function
class Class1(SomeBaseClass):
def method1(self, arg1, arg2):
external_function(param)
Now I have test code: 现在我有测试代码:
import mock
from django.test import TestCase
from mymodule import class1
class Class1Test(TestCase)
def test_method1:
'''how can I mock external_function here?'''
You would write: 你会写:
class Class1Test(TestCase):
@mock.patch('mymodule.external_function')
def test_method1(self, mock_external_function):
pass
Looking at mymodule
the function external_function
is directly imported. 查看
mymodule
,直接导入函数external_function
。 Hence you need to mock mymodule.external_function
as that's the function that will be called when method1
is executed. 因此,您需要模拟
mymodule.external_function
因为这是执行method1
时将调用的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.