模拟补丁功能



我是python模拟的新手,最近我正在尝试为我的函数编写测试代码。该函数用于分析 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
但它没有用。所以我的问题是:

  1. 这是使用补丁的正确方法吗,如果不是,我该如何修补它?

  2. 在mongo_report_test(mock_call),mock_call从何而来,它与这里的任何内容都没有链接,我该如何声明它

  3. 打完补丁后,如何调用这个测试函数?

开始向装饰师学习,花了一整天的时间把头撞到桌子上,但仍然没有学会<诀窍。>

我花了一段时间才发现最初的答案具有误导性。这是一个与您的问题非常相似的 mock 的工作副本,我唯一不知道的是您的返回值 get_connection()。蟒蛇 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

除非这是一些剪切粘贴错误,否则我相信这一点:

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}]

最新更新