1
幾次我遇到了修飾器unittest.mock.patch
的問題。當我嘗試從模塊中模擬單個功能時,patch
不起作用。但是,如果從包含模塊的功能收集爲類方法,patch
完美工作。蟒蛇模擬修補程序修飾器對於類方法和個別功能的行爲不同
部分地這question與我的相交。但是這個問題也沒有很好的答案。
這裏是什麼,我想描述一個例子:
|-- __init__.py
|-- helpers.py
|-- main_module.py
|-- tests.py
我helpers.py
寫一個函數作爲一個類的方法,而另一個作爲一個獨立的功能:
# helpers.py
class HelperClass():
def method_a(self):
return "a"
def function_a():
return "a"
我已經將它們包括在主模塊中:
# main_module.py
from helpers import HelperClass, function_a
def function_which_uses_helper_function():
a_val = function_a()
return a_val
def function_which_uses_helper_class_method():
a_val = HelperClass().method_a()
return a_val
最後t EST序列:
# tests.py
from unittest import TestCase
from unittest.mock import patch
from main_module import function_which_uses_helper_function, function_which_uses_helper_class_method
class TestClass(TestCase):
@patch('helpers.function_a')
def test_function_which_uses_helper_function(self, mock_function_a):
mock_function_a.return_value = "c"
self.assertEqual(function_which_uses_helper_function(), "c")
@patch('helpers.HelperClass.method_a')
def test_function_which_uses_helper_class_method(self, mock_method_a):
mock_method_a.return_value = "c"
self.assertEqual(function_which_uses_helper_class_method(), "c")
這給了我這些結果:
$ py.test tests.py
<...>
tests.py .F
<...>
tests.py:11: in test_function_which_uses_helper_function
self.assertEqual(function_which_uses_helper_function(), "c")
E AssertionError: 'a' != 'c'
E - a
E + c
============ 1 failed, 1 passed in 0.14 seconds ============
我感謝所有幫助。希望這也有助於某人:)
如果您將類中的'function_a'重命名爲'method_a',它可能會清除一些事情。 – jonrsharpe
感謝您的建議@jonrsharpe!完成。 – AnaPana