使用pytest模拟函数的参数

Mocking parameters of the function using pytest

class_functions.py

#create object called some_application
#some_application is an object, and it's current session of an application
some_application=app.SessionStart()
class_functions:
    def PushScreenValue(some_application,"selectedvalue"):
        if some_application.header=="this_screen":
           some_application.send_values("selectvalue")
           some_application.selectAll()
           return True
        else:
         return False


How can I mock the parameter pytest python
test_class.py

#这里我正在模拟作为 parameter.I 发送的 some_application 对象我也在模拟 some_application.header、some_application.send_values 和 some_application.selectAll

from class_functions import class_functions



@pytest.mark.parametrize("some_application,"Value",expected", [(some_application,"Value",True)])
@mock.patch("class_functions.class_functions.mock_some_application.header")
@mock.patch("class_functions.class_functions.mock_some_application.send_values")
@mock.patch("class_functions.class_functions.mock_some_application.selectAll")
def test_PushScreenValue(mock_some_application,mock_some_application_header,mock_some_application_select,selectedvalue,expected):
  
  mock_some_application_header.return_value="this_screen"
  selectedvalue="Value"
   mock_some_application_send_values(selectedvalue)  #Sends the value to the app window
   mock_some_application_selectAll() #Selects the app window
   #checks there the function is rurning expected value
   assert class_functions_object.PushScreenValue(mock_some_application,selectedvalue)==expected
     

我不确定如何在测试函数中正确传递这些模拟对象

首先,您尝试测试的 class 似乎有语法错误。我认为它可能如下所示:

class functions:
    def PushScreenValue(self, some_application, val):
        if some_application.header=="this_screen":
            some_application.send_values(val)
            some_application.selectAll()
            return True
        else:
            return False

现在进行测试,我们不需要创建一堆打补丁的对象。我们只需要一个,用于 some_application 因为它有方法调用。在那种情况下,我们可以简单地使用 MagicMock,它只是 Mock.

的子 class

至于测试,我们只有两个逻辑分支要测试,一个是returns True,一个是returns False。剩下的就是设置我们的两个测试来命中每个分支并在此过程中做出断言。在测试函数 returns True 时,我们可以做出的一个可能断言是查看 some_application 是否确实使用我们传入的值调用了 send_values。如下图所示测试您的功能并命中两个逻辑分支的两个测试。

import pytest
from unittest.mock import MagicMock

def test_pushscreen_success():
    klass = functions()
    app = MagicMock()
    app.header = "this_screen"

    screen_val = klass.PushScreenValue(app, "fizz")

    app.send_values.assert_called_with("fizz")
    app.selectAll.assert_called_once()
    assert screen_val


def test_pushscreen_fail():
    klass = functions()
    app = MagicMock()
    app.header = "foo"

    screen_val = klass.PushScreenValue(app, "fizz")
    assert screen_val is False

当我 运行 它时,我得到以下输出:

===================================== test session starts =====================================
platform darwin -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: *******
collected 2 items                                                                             

test_foo.py ..                                                                          [100%]

====================================== 2 passed in 0.06s ======================================