Python Mock.Patch 在多个地方使用的函数

Rom*_*omn 4 python unit-testing mocking python-unittest.mock

这有效:

我得到了我的程序:

# module/core_functions.py

def new_input(question):
    print(question)
    value = input(">").strip()
    return value


def main_function():
    #do things
    new_input("question1")
    #do other things
    new_input("question2")
    ...
Run Code Online (Sandbox Code Playgroud)

我写了一个单元测试:

import unittest
from unittest.mock import patch, Mock
from module.core_functions import main_function

class MytestClass(unittest.TestCase):

    @patch('module.core_functions.new_input')
    def test_multiple_answer(self, mock_input):
        mock_input.return_value = Mock()
        mock_input.side_effect = ['Answer1', 'Answer2']
        result = main_function()
        self.assertIn('ExpectedResult', result)
Run Code Online (Sandbox Code Playgroud)

这工作得很好(我用来nose2运行所有测试)。

现在这不起作用:

随着我的代码变得越来越大,我想让其他人参与我的项目,并且我需要模块化我的功能以使修改更容易和更干净。

所以我将该new_input函数放入一个新文件中module/io.py,并得到了一个新的子函数:

# module/subfunctions.py
from module.io import new_input

def subfunction():
    # do things
    new_input("question")
    # do things
    return things
Run Code Online (Sandbox Code Playgroud)

我的核心计划演变为:

# module/core_functions.py
from module.io import new_input
from module.subfunctions import subfunction

def main_function():
    #do things
    new_input("question1")
    #do other things
    subfuntion()
    ...
Run Code Online (Sandbox Code Playgroud)

所以问题是要模拟的函数位于多个地方:在主函数和一些子函数中。我找不到一种方法让我的单元测试工作(我无法预测主函数或一个子函数是否需要 AnswerX)。

您知道如何修复我的测试以使其正常工作吗?谢谢(我希望我已经说得相当清楚了)。

编辑:

我尝试过类似的东西:

@patch('module.core_functions.new_input')
@patch('module.subfunctions.new_input')
def test_multiple_answer(self, mock_input):
    mock_input.return_value = Mock()
    mock_input.side_effect = ['Answer1', 'Answer2']
    result = main_function()
    self.assertIn('ExpectedResult', result)
Run Code Online (Sandbox Code Playgroud)

但我得到了错误:TypeError: test_multiple_answer() takes 2 positional arguments but 3 were given

Mar*_*rkM 6

我刚刚问了自己同样的问题,虽然我很确定答案,但我写了一个愚蠢的小但可以理解的概念证明。

就像@whats-done-is 指出的那样,有两种方法可以解决这个问题;mock.patch()使用的new参数指定要内联的模拟对象,或者为每个 @patch 注释添加一个参数。

实用程序.py

def expensive_function():
    return 12345
Run Code Online (Sandbox Code Playgroud)

模块1.py

from utils import expensive_function
from module2 import method2

def method1():
    return {'method1': expensive_function(), 'method2': method2()}
Run Code Online (Sandbox Code Playgroud)

模块2.py

from utils import expensive_function

def method2():
    return expensive_function()
Run Code Online (Sandbox Code Playgroud)

主要.py

import unittest
from unittest.mock import patch
from module1 import method1

def mock_expensive_function():
    return 123

class TestCases(unittest.TestCase):
    def test_unpatched(self):
        self.assertEqual({'method1': 12345, 'method2': 12345}, method1())

    @patch('module1.expensive_function', new=mock_expensive_function)
    def test_method1_patched(self):
        self.assertEqual({'method1': 123, 'method2': 12345}, method1())

    @patch('module2.expensive_function', new=mock_expensive_function)
    def test_method2_patched(self):
        self.assertEqual({'method1': 12345, 'method2': 123}, method1())

    @patch('module1.expensive_function', new=mock_expensive_function)
    @patch('module2.expensive_function', new=mock_expensive_function)
    def test_both_patched_inline(self):
        self.assertEqual({'method1': 123, 'method2': 123}, method1())

    @patch('module1.expensive_function')
    @patch('module2.expensive_function')
    def test_both_patched_magicmock(self, mock_in_module2, mock_in_module1):
        mock_in_module1.return_value = mock_expensive_function()
        mock_in_module2.return_value = mock_expensive_function()
        self.assertEqual({'method1': 123, 'method2': 123}, method1())

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)