如何为方法应用 @mock.patch 并在一个或多个测试中保持对原始方法的访问?
How to apply @mock.patch for method and keep access to original method within one or more Tests?
在为 python 应用程序进行单元测试时,当 mock.patch
通过 start()
方法激活模拟时,我遇到了一个有趣的案例。要重现问题,请将以下代码放入两个文件 tests.py
和 utils.py
并将它们放在一个文件夹下:
tests.py:
import mock
import unittest
import utils
class TestA(unittest.TestCase):
def setUp(self):
pass
def test_method_a(self):
mock.patch('utils.method_b', return_value=None).start()
actual_result = utils.method_a()
# Assertion code
def test_method_b(self):
actual_result = utils.method_b()
self.assertTrue(actual_result is None)
utils.py:
def method_a():
print 'A Method'
return method_b()
def method_b():
print 'B Method'
return True
注意 test_method_a
mocking app_utils.method_b
和 test_method_b
将调用原始 app_utils.method_b
。我遇到了 test_method_b
无法调用实际 app_utils.method_b
的情况,因为它被 test_method_a
嘲笑了。
我知道解决该问题的方法很少:
- 使用
from app.utils import test_method_b
, - 所以方法会留在我的 test.py 的命名空间中
- 通过
with
语句使用 mock.patch
作为上下文管理器。
- 使用
mock.patch
作为装饰器。
问题是如果不应用上述解决方案是否可以解决问题?
如果可能的话,我需要继续使用 mock.patch
和 imports
。
上面描述的奇怪行为是由于没有停止模拟 method_b.
的修补程序造成的
查看下面的工作代码:
utils.py
文件与问题相同。
tests.py
:
import mock
import unittest
import utils
class TestA(unittest.TestCase):
def setUp(self):
pass
def test_method_a(self):
method_b_patcher = mock.patch('utils.method_b', return_value=None)
method_b_patcher.start()
actual_result = utils.method_a()
method_b_patcher.stop()
def test_method_b(self):
actual_result = utils.method_b()
self.assertTrue(actual_result)
所以更新的是,在 test_method_a
中,我添加了 method_b_patcher
并在 test_method_a
方法中为该修补程序调用了 start
和 stop
。
在为 python 应用程序进行单元测试时,当 mock.patch
通过 start()
方法激活模拟时,我遇到了一个有趣的案例。要重现问题,请将以下代码放入两个文件 tests.py
和 utils.py
并将它们放在一个文件夹下:
tests.py:
import mock
import unittest
import utils
class TestA(unittest.TestCase):
def setUp(self):
pass
def test_method_a(self):
mock.patch('utils.method_b', return_value=None).start()
actual_result = utils.method_a()
# Assertion code
def test_method_b(self):
actual_result = utils.method_b()
self.assertTrue(actual_result is None)
utils.py:
def method_a():
print 'A Method'
return method_b()
def method_b():
print 'B Method'
return True
注意 test_method_a
mocking app_utils.method_b
和 test_method_b
将调用原始 app_utils.method_b
。我遇到了 test_method_b
无法调用实际 app_utils.method_b
的情况,因为它被 test_method_a
嘲笑了。
我知道解决该问题的方法很少:
- 使用
from app.utils import test_method_b
, - 所以方法会留在我的 test.py 的命名空间中
- 通过
with
语句使用mock.patch
作为上下文管理器。 - 使用
mock.patch
作为装饰器。
问题是如果不应用上述解决方案是否可以解决问题?
如果可能的话,我需要继续使用 mock.patch
和 imports
。
上面描述的奇怪行为是由于没有停止模拟 method_b.
的修补程序造成的查看下面的工作代码:
utils.py
文件与问题相同。
tests.py
:
import mock
import unittest
import utils
class TestA(unittest.TestCase):
def setUp(self):
pass
def test_method_a(self):
method_b_patcher = mock.patch('utils.method_b', return_value=None)
method_b_patcher.start()
actual_result = utils.method_a()
method_b_patcher.stop()
def test_method_b(self):
actual_result = utils.method_b()
self.assertTrue(actual_result)
所以更新的是,在 test_method_a
中,我添加了 method_b_patcher
并在 test_method_a
方法中为该修补程序调用了 start
和 stop
。