【问题标题】:mock patch function模拟补丁功能
【发布时间】:2017-01-11 23:51:46
【问题描述】:

我是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上学到的东西写的
但它没有用。所以我的问题是:

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

  2. 在mongo_report_test(mock_call),mock_call是从哪里来的,这里什么都没有关联,怎么声明呢

  3. patch后如何调用这个测试函数?

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

【问题讨论】:

  • 这看起来有点迂回,但对我来说似乎可行。 (你在mock_get_collection 中使用side_effect 看起来有点可疑,但除此之外......)你能更清楚地解释一下你所说的“它不起作用”是什么意思吗?
  • 我想我的意思是我不知道如何在补丁之后调用它们。当我调用 mongo_report_test 时,我需要传递“mock_call”参数,对,但什么是 mock_call,真的吗?我不明白。是另一个“mock_call = Mock()”吗?

标签: python python-2.7 unit-testing mocking patch


【解决方案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

【讨论】:

  • 我猜第二个解决方案看起来正是我所需要的。谢谢!我得到的另一个问题是我怎么称呼它?如果我调用 test_from_report,我缺少我需要传递的参数,对吗? “mock_get_collection”那个?
  • @RobinsonWei,这个mock不好解释,之前的代码我测试后不行,这个应该可以的。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 2011-12-26
    • 1970-01-01
    • 2020-11-26
    • 2020-10-16
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多