我是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上学到的内容编写 但它没有用。所以我的问题是:
这是使用补丁的正确方法,如果没有,我该如何修补它?
在mongo_report_test(mock_call),mock_call来自哪里,它没有链接到这里的任何内容,我该如何声明呢
补丁后,如何调用此测试功能?
开始向装饰师学习,花了一整天的时间撞到桌子上,但仍然没有学到诀窍。 :其中
答案 0 :(得分:1)
我花了一段时间才弄清楚原来的答案是误导性的。这是一个非常类似于你的问题的模拟工作副本,我唯一不知道的是你的get_connection()的返回值。 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
答案 1 :(得分:0)
除非这是一些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}]