python - 模拟补丁功能

标签 python python-2.7 unit-testing mocking patch

我是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. 打完补丁后,如何调用这个测试函数?

开始向decorator学习,头撞 table 一整天,还是没有学会窍门。 :<

最佳答案

我花了一段时间才发现原来的答案具有误导性。这是与您的问题非常相似的模拟工作副本,我唯一不知道的是 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

关于python - 模拟补丁功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41602875/

相关文章:

python - 如何使用 Scrapy 在一个 POST 请求上捕获多个响应?

python - python 3中的快速异或字节

python - Python 中的枚举 : How to enforce in method arguments

Angular Testing 无法读取未定义的属性 'name'

python - Django 2.x drf-yasg 如何在自定义方法中创建 API(如 Swagger )

python - 快速入门 Flask 应用程序因某种原因失败

vb.net - 如何模拟 OleDbDataAdapter(query, connStr)

c# - 为什么 visual studio 2012 找不到我的测试?

python - Py.test fixture : Use function fixture in scope fixture

python - 链表 Python