如何模拟 Python class 的对象?
How to mock objects of a Python class?
让我说以下 class;
class CompositionClass(object):
def __init__(self):
self._redis = Redis()
self._binance_client = BinanceClient()
def do_processing(self, data):
self._redis.write(data)
self._binance_client.buy(data.amount_to_buy)
# logic to actually unittest
return process_val
我有其他对象在我的 ComplexClass
中调用外部 API 作为组合。当我对 do_processing
的逻辑进行单元测试时,我不想调用这些昂贵的 API 调用。我已经在 SO 和 Google 中彻底检查了单元测试;所有的例子都很简单,并不是很有用。就我而言,如何使用 unittest.mock
来模拟这些对象?
您需要设置一个值,该值必须由您的 API 调用返回到此函数:
from unittest.mock import MagicMock
class Tester(unittest.TestCase):
def setUp(self):
pass
def test_do_processing(self):
self.API_function = MagicMock(return_value='API_response')
# test logic
模拟 Redis
和 BinanceClient
classes 的一种方法是在测试 class 中使用 patch
装饰器,例如:
from unittest import TestCase
from unittest.mock import patch
from package.module import CompositionClass
class TestCompositionClass(TestCase):
@patch('package.module.BinanceClient')
@patch('package.module.Redis')
def test_do_processing(self, mock_redis, mock_binance):
c = CompositionClass()
data = [...]
c.do_processing(data)
# Perform your assertions
# Check that mocks were called
mock_redis.return_value.write.assert_called_once_with(data)
mock_binance.return_value.buy.assert_called_once_with(data.amount_to_buy)
请注意,指定给 @patch
的路径是包含 CompositionClass
及其对 Redis
和 BinanceClient
的导入的模块的路径。修补发生在该模块中,而不是包含 Redis
和 BinanceClient
实现本身的模块。
让我说以下 class;
class CompositionClass(object):
def __init__(self):
self._redis = Redis()
self._binance_client = BinanceClient()
def do_processing(self, data):
self._redis.write(data)
self._binance_client.buy(data.amount_to_buy)
# logic to actually unittest
return process_val
我有其他对象在我的 ComplexClass
中调用外部 API 作为组合。当我对 do_processing
的逻辑进行单元测试时,我不想调用这些昂贵的 API 调用。我已经在 SO 和 Google 中彻底检查了单元测试;所有的例子都很简单,并不是很有用。就我而言,如何使用 unittest.mock
来模拟这些对象?
您需要设置一个值,该值必须由您的 API 调用返回到此函数:
from unittest.mock import MagicMock
class Tester(unittest.TestCase):
def setUp(self):
pass
def test_do_processing(self):
self.API_function = MagicMock(return_value='API_response')
# test logic
模拟 Redis
和 BinanceClient
classes 的一种方法是在测试 class 中使用 patch
装饰器,例如:
from unittest import TestCase
from unittest.mock import patch
from package.module import CompositionClass
class TestCompositionClass(TestCase):
@patch('package.module.BinanceClient')
@patch('package.module.Redis')
def test_do_processing(self, mock_redis, mock_binance):
c = CompositionClass()
data = [...]
c.do_processing(data)
# Perform your assertions
# Check that mocks were called
mock_redis.return_value.write.assert_called_once_with(data)
mock_binance.return_value.buy.assert_called_once_with(data.amount_to_buy)
请注意,指定给 @patch
的路径是包含 CompositionClass
及其对 Redis
和 BinanceClient
的导入的模块的路径。修补发生在该模块中,而不是包含 Redis
和 BinanceClient
实现本身的模块。