使用pytest模拟函数的参数

2024-04-26 14:50:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

#这里我模拟一些作为参数发送的_应用程序对象。我还模拟一些_application.header、一些_application.send和一些_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
     

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


Tags: thesendappobjectapplicationsomefunctionsmock
1条回答
网友
1楼 · 发布于 2024-04-26 14:50:04

首先,您尝试测试的类似乎有语法错误。我认为它可能是这样的:

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,因为它有方法调用。在这种情况下,我们可以简单地使用^{},它只是Mock的一个子类

至于测试,我们只有两个逻辑分支需要测试,一个返回True,另一个返回False。剩下的就是我们要设置两个测试来命中这些分支中的每一个,并在此过程中做出断言。在测试函数是否返回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 ======================================

相关问题 更多 >