[英]How can I mock an external function within a method in a class
我需要一些關於模擬的幫助。
我在mymodule.py中有以下代碼:
from someModule import external_function
class Class1(SomeBaseClass):
def method1(self, arg1, arg2):
external_function(param)
現在我有測試代碼:
import mock
from django.test import TestCase
from mymodule import class1
class Class1Test(TestCase)
def test_method1:
'''how can I mock external_function here?'''
你會寫:
class Class1Test(TestCase):
@mock.patch('mymodule.external_function')
def test_method1(self, mock_external_function):
pass
查看mymodule
,直接導入函數external_function
。 因此,您需要模擬mymodule.external_function
因為這是執行method1
時將調用的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.