如何只模拟类中的方法

如何只模拟类中的方法

本文介绍了如何只模拟类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为我的基于类的函数编写测试用例.这是我班的骨架

类库(对象):def get_file(self):经过def query_fun(self):经过def get_response(self):self.get_file()响应 = self.query_fun()# 这里有一些业务逻辑返回响应

我需要创建一个测试用例,它可以只模拟 query_fun 并完成其余的工作.在下面尝试过,但似乎不是正确的方向:

from unittest import mock, TestCasefrom library.library 导入库类 TestLibrary(TestCase):定义设置(自我):self.library = Library()@mock.patch('library.library.Library')def test_get_response(self, MockLibrary):模拟响应 = {'文本':'你好','实体':'价值',}MockLibrary.query_fun.return_value = mock_response响应 = MockLibrary.get_response()self.assertEqual(响应,模拟响应)

我期望的是设置模拟并调用 get_response 实际上不会调用原始的 query_fun 方法,而是调用模拟 query_fun>.

解决方案

你需要考虑到你模拟了整个类.您也可以模拟单个方法:

@mock.patch('library.library.Library.query_fun')def test_get_response(self, mock_query_fun):mock_query_fun.return_value = {'文本':'你好',实体":价值"}}响应 = MockBotter.get_response()self.assertIsNotNone(响应)

只有 Library.query_fun 已被替换,类中的其他所有内容仍然存在.

简化演示:

>>>从单元测试导入模拟>>>类库(对象):... def query_fun(self):... 经过... def get_response(self):...返回 self.query_fun()...>>>使用 mock.patch('__main__.Library.query_fun') 作为 mock_query_fun:... mock_query_fun.return_value = {'foo': 'bar'}...打印(图书馆().get_response())...{'foo': 'bar'}

Trying to write a testcase for my class based function. This is skeleton of my class

class Library(object):
    def get_file(self):
        pass

    def query_fun(self):
        pass

    def get_response(self):
        self.get_file()

        response = self.query_fun()

        # some business logic here
        return response

I need to create a testcase that can mock just the query_fun and do the rest. tried below but seems is not the right direction:

from unittest import mock, TestCase
from library.library import Library

class TestLibrary(TestCase):
    def setUp(self):
        self.library = Library()

    @mock.patch('library.library.Library')
    def test_get_response(self, MockLibrary):
        mock_response = {
            'text': 'Hi',
            'entities': 'value',
        }
        MockLibrary.query_fun.return_value = mock_response
        response = MockLibrary.get_response()
        self.assertEqual(response, mock_response)

What I'm expecting is to setup mock and calling get_response will not actually call the original query_fun method, but instead call the mock query_fun.

解决方案

You need to take into account you mocked the whole class. You can mock individual methods too:

@mock.patch('library.library.Library.query_fun')
def test_get_response(self, mock_query_fun):
    mock_query_fun.return_value = {
        'text': 'Hi',
        'entities': 'value'
        }
    }
    response = MockBotter.get_response()
    self.assertIsNotNone(response)

Only Library.query_fun has been replaced, everything else in the class is still in place.

Simplified demo:

>>> from unittest import mock
>>> class Library(object):
...     def query_fun(self):
...         pass
...     def get_response(self):
...         return self.query_fun()
...
>>> with mock.patch('__main__.Library.query_fun') as mock_query_fun:
...     mock_query_fun.return_value = {'foo': 'bar'}
...     print(Library().get_response())
...
{'foo': 'bar'}

这篇关于如何只模拟类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:28