【问题标题】:Mock a function with parameters that is indirectly called with a mock function instead使用模拟函数间接调用的参数模拟函数
【发布时间】:2022-08-23 02:46:15
【问题描述】:

我一直在尝试测试一个使用一些参数调用另一个函数的函数。我正在尝试模拟最新的,以便它不会实际运行,而是执行一个返回一些模拟值的模拟函数。

我所拥有的 - 简化 - 看起来像这样:

def function_to_test():
    a = 2
    b = 3
    c = 4
    results = second_function(a, b, c)
    return results

然后我试图模拟的函数如下所示:

def second_function(a, b , c):
    a = b + c
    return a 

function_to_testsecond_function 都属于 class Example

我正在使用unittest 进行测试,不幸的是我无法切换到 pytest,因此没有 pytest 选项有帮助。

到目前为止,我在测试中所做的是:

@patch(\'rootfolder.subfolder.filename.Example.second_function\', autospec=True)
def test_function_to_test(self, get_content_mock):
    get_content_mock.return_value = mocked_second_function()

    res = function_to_test()
    self.assertEqual(res, 10)

如你看到的我正在尝试使用模拟函数而不是实际的second_function看起来像这样:

def mocked_second_function(a, b, c):
    # using a, b, c for other actions
    # for the question I will just print them but they are actually needed
    print(f\"{a}, {b}, {c}\")
    return 10

问题是当我设置get_content_mock.return_value = mocked_second_function().

我需要传递参数,但在我的实际问题中,这些参数是在function_to_test 处生成的所以我无法事先知道他们。

我阅读了许多相关的问题和文档,但似乎找不到可以解决我的问题的东西。任何帮助甚至不同的方法都会有所帮助。

    标签: unit-testing mocking python-unittest python-unittest.mock


    【解决方案1】:

    使用 unittest 库有assert_called_with。如果这与MagicMock 功能一起使用,那么您可以在不运行实际second_function 的情况下测试first_function,但测试它是否已使用正确的参数调用。

    这是一个使用正确和错误参数的示例测试:

    import unittest
    from unittest.mock import MagicMock
    
    class Example:
        def first_function(self):
            a = 2
            b = 3
            c = 4
            results = self.second_function(a, b, c)
            return results
    
        def second_function(self, a, b , c):
            a = b + c
            return a
    
    class TestMockSecondMethod(unittest.TestCase):
    
        def test_func_dut_pass(self):
            example = Example()
            example.second_function = MagicMock()
            result = example.first_function()
            example.second_function.assert_called_with(2, 3, 4)
    
        def test_func_dut_fail(self):
            example = Example()
            example.second_function = MagicMock()
            result = example.first_function()
            example.second_function.assert_called_with(1, 3, 4)
    
    
    if __name__ == '__main__':
        unittest.main()
    

    这给出了以下输出:

    $ python3.6 -m unittest --v so_unittest_mock.py 
    test_func_dut_fail (so_unittest_mock.TestMockSecondMethod) ... FAIL
    test_func_dut_pass (so_unittest_mock.TestMockSecondMethod) ... ok
    
    ======================================================================
    FAIL: test_func_dut_fail (so_unittest_mock.TestMockSecondMethod)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/user1/so_unittest_mock.py", line 28, in test_func_dut_fail
        example.second_function.assert_called_with(1, 3, 4)
      File "/usr/lib/python3.6/unittest/mock.py", line 814, in assert_called_with
        raise AssertionError(_error_message()) from cause
    AssertionError: Expected call: mock(1, 3, 4)
    Actual call: mock(2, 3, 4)
    
    ----------------------------------------------------------------------
    Ran 2 tests in 0.003s
    
    FAILED (failures=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-22
      • 2022-07-26
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      相关资源
      最近更新 更多