模拟补丁功能
mock patch function
我是python mock 的新手,最近我正在尝试为我的功能编写测试代码。
该函数用于分析 Mongo 数据库和 CSV 报告。我想修补 "get_collection()" 函数。结构是这样的:
mongo_report.py 文件:
import pymongo
def get_collection(): # used to get mongo collection
def from_report(): #used to from a report
get_collection(mongodb, mongo_collection_name)
.....
在我的测试文件中:
from mongo_report import from_report
from mock import Mock, patch
def mock_get_collection(): # used to replace get_collection()
mocked_collection = Mock()
mock_get_collection.count.side_effect = [20, 6, 2]
mock_get_collection.find.side_effect = [{user: xx}]
return mocked_collection
@patch('mongo_report.get_collection')
def mongo_report_test(mock_call):
mock_call.return_value = mock_get_collection()
from_report()
补丁是我在网页上学到的东西写的https://blog.fugue.co/2016-02-11-python-mocking-101.html
但它没有 work.So 我的问题是:
这样打补丁正确吗,如果不正确,怎么打补丁?
在mongo_report_test(mock_call),mock_call是从哪里来的,这里没有链接到任何东西,我怎么声明它
打补丁后,这个测试函数怎么调用?
从decorator开始学,脑袋撞了一天,还是没学会窍门。 :<
除非这是一些 cut-n-paste 错误,否则我相信:
mock.count.side_effect = [20, 6, 2]
mock.find.side_effect = [{user: xx}]
应该是:
mocked_collection.count.side_effect = [20, 6, 2]
mocked_collection.find.side_effect = [{user: xx}]
我花了一段时间才发现原来的答案具有误导性。这是模拟的工作副本,与您的问题非常相似,我唯一不知道的是 get_connection() 的 return 值。 python v2.6.6
#/usr/bin/env pthon
import unittest
import mock
from mock import MagicMock
class Mongo(object):
def __init__(self, *args, **kwargs):
# doing nothing for demo
pass
def get_collection(self, input1):
# return fixed list for demo
return [1, 3, 5]
def from_report(self, input_a, input_b):
collection_a = self.get_collection(input_a)
collection_b = self.get_collection(input_b)
return collection_a + collection_b
class TestMongo(unittest.TestCase):
def setUp(self):
self.expected = [2, 4, 6, 2, 4, 6]
self.mock_value = [2, 4, 6]
def tearDown(self):
pass
""" in the format of file_name.class_name.method_name
@mock.patch('test_mongo.Mongo.get_collection')
def test_using_mock_patch(self, mock_get_collection):
mock_get_collection.return_value = self.mock_value
mongo = Mongo()
mongo.get_collection = mock_get_collection
result = mongo.from_report('any', 'where')
self.assertEquals(result, self.expected)
""" mock.MagicMock
def test_mongo_get_collection_using_magicMock(self):
mock_mongo = MagicMock(name='get_collection')
mock_mongo.get_collection.return_value = self.mock_value
mongo = Mongo()
mongo.get_collection = mock_mongo.get_collection
result = mongo.from_report('any', 'where')
self.assertEquals(result, self.expected)
""" mock.Mock
def test_mongo_get_collection_using_Mock(self):
mock_mongo = mock.Mock(name='get_collection')
mock_mongo.get_collection.return_value = self.mock_value
mongo = Mongo()
mongo.get_collection = mock_mongo.get_collection
result = mongo.from_report('any', 'where')
self.assertEquals(result, self.expected)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestMongo)
unittest.TextTestRunner(verbosity=2).run(suite)
这是命令和输出
-bash-4.1$ python test_mongo.py
test_mongo_get_collection_using_Mock (__main__.TestMongo) ... ok
test_mongo_get_collection_using_magicMock (__main__.TestMongo) ... ok
test_using_mock_patch (__main__.TestMongo) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.005s
OK
-bash-4.1$ nosetests -vv test_mongo.py
我是python mock 的新手,最近我正在尝试为我的功能编写测试代码。 该函数用于分析 Mongo 数据库和 CSV 报告。我想修补 "get_collection()" 函数。结构是这样的:
mongo_report.py 文件:
import pymongo
def get_collection(): # used to get mongo collection
def from_report(): #used to from a report
get_collection(mongodb, mongo_collection_name)
.....
在我的测试文件中:
from mongo_report import from_report
from mock import Mock, patch
def mock_get_collection(): # used to replace get_collection()
mocked_collection = Mock()
mock_get_collection.count.side_effect = [20, 6, 2]
mock_get_collection.find.side_effect = [{user: xx}]
return mocked_collection
@patch('mongo_report.get_collection')
def mongo_report_test(mock_call):
mock_call.return_value = mock_get_collection()
from_report()
补丁是我在网页上学到的东西写的https://blog.fugue.co/2016-02-11-python-mocking-101.html
但它没有 work.So 我的问题是:
这样打补丁正确吗,如果不正确,怎么打补丁?
在mongo_report_test(mock_call),mock_call是从哪里来的,这里没有链接到任何东西,我怎么声明它
打补丁后,这个测试函数怎么调用?
从decorator开始学,脑袋撞了一天,还是没学会窍门。 :<
除非这是一些 cut-n-paste 错误,否则我相信:
mock.count.side_effect = [20, 6, 2]
mock.find.side_effect = [{user: xx}]
应该是:
mocked_collection.count.side_effect = [20, 6, 2]
mocked_collection.find.side_effect = [{user: xx}]
我花了一段时间才发现原来的答案具有误导性。这是模拟的工作副本,与您的问题非常相似,我唯一不知道的是 get_connection() 的 return 值。 python v2.6.6
#/usr/bin/env pthon
import unittest
import mock
from mock import MagicMock
class Mongo(object):
def __init__(self, *args, **kwargs):
# doing nothing for demo
pass
def get_collection(self, input1):
# return fixed list for demo
return [1, 3, 5]
def from_report(self, input_a, input_b):
collection_a = self.get_collection(input_a)
collection_b = self.get_collection(input_b)
return collection_a + collection_b
class TestMongo(unittest.TestCase):
def setUp(self):
self.expected = [2, 4, 6, 2, 4, 6]
self.mock_value = [2, 4, 6]
def tearDown(self):
pass
""" in the format of file_name.class_name.method_name
@mock.patch('test_mongo.Mongo.get_collection')
def test_using_mock_patch(self, mock_get_collection):
mock_get_collection.return_value = self.mock_value
mongo = Mongo()
mongo.get_collection = mock_get_collection
result = mongo.from_report('any', 'where')
self.assertEquals(result, self.expected)
""" mock.MagicMock
def test_mongo_get_collection_using_magicMock(self):
mock_mongo = MagicMock(name='get_collection')
mock_mongo.get_collection.return_value = self.mock_value
mongo = Mongo()
mongo.get_collection = mock_mongo.get_collection
result = mongo.from_report('any', 'where')
self.assertEquals(result, self.expected)
""" mock.Mock
def test_mongo_get_collection_using_Mock(self):
mock_mongo = mock.Mock(name='get_collection')
mock_mongo.get_collection.return_value = self.mock_value
mongo = Mongo()
mongo.get_collection = mock_mongo.get_collection
result = mongo.from_report('any', 'where')
self.assertEquals(result, self.expected)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestMongo)
unittest.TextTestRunner(verbosity=2).run(suite)
这是命令和输出
-bash-4.1$ python test_mongo.py
test_mongo_get_collection_using_Mock (__main__.TestMongo) ... ok
test_mongo_get_collection_using_magicMock (__main__.TestMongo) ... ok
test_using_mock_patch (__main__.TestMongo) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.005s
OK
-bash-4.1$ nosetests -vv test_mongo.py