Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python模块pytest mock如何在一个类调用另一个类实例时测试方法调用参数?_Python_Unit Testing_Python Mock_Pytest Mock - Fatal编程技术网

Python模块pytest mock如何在一个类调用另一个类实例时测试方法调用参数?

Python模块pytest mock如何在一个类调用另一个类实例时测试方法调用参数?,python,unit-testing,python-mock,pytest-mock,Python,Unit Testing,Python Mock,Pytest Mock,几天前,我开始使用pytest学习单元测试,并对pytest模拟插件()感兴趣 我已经能够编写相当多的单元测试来测试我的代码,但现在我想测试我的部分代码如何与其他对象交互。假设我有类B,这是我感兴趣测试的类,它有一个方法将调用类a中的方法,我想断言,当类B调用类a时,方法调用是使用预期参数进行的 我总结了一些能够完成任务的黑客示例(见下面的链接),但正如您所见,这可能不是正确的方法。所以我的问题是,如何通过使用pythonmock或pytestmock模块正确地处理这个问题?不基于pytest模

几天前,我开始使用pytest学习单元测试,并对pytest模拟插件()感兴趣

我已经能够编写相当多的单元测试来测试我的代码,但现在我想测试我的部分代码如何与其他对象交互。假设我有类B,这是我感兴趣测试的类,它有一个方法将调用类a中的方法,我想断言,当类B调用类a时,方法调用是使用预期参数进行的

我总结了一些能够完成任务的黑客示例(见下面的链接),但正如您所见,这可能不是正确的方法。所以我的问题是,如何通过使用pythonmock或pytestmock模块正确地处理这个问题?不基于pytest模拟插件的答案也很受欢迎

在下面的代码中,我不喜欢断言,因为我更喜欢使用现有的pytest mock“assert_called_once_with(…)”-方法,但我就是无法让它工作。mock对象中提供了所有信息,但我不明白在这种情况下如何正确使用pytest mock API

def test_how_class_b_interact_with_class_a(mocker):
    class A(object):
        def further_process_nbr(self, nbr):
            pass # don't care for the sake of this example

    class B(object):
        def __init__(self):
            self.a = A()

        def process_nbr(self, nbr):
            nbr += 2 # do something fancy with number and then have it further processed by object a
            return self.a.further_process_nbr(nbr)

    nbr = 4
    expected = 6

    # Prepare
    mock_a = mocker.patch.object(A, 'further_process_nbr', autospec=True)

    # Exercise
    B().process_nbr(nbr)

    # Assert
    assert mock_a.call_args_list[0].args[1] == expected # This is working, but not exactly a nice way
    # mock_a.assert_called_once_with(expected) something like this is basically what I would like to do

实际上,您看到的结果是正确的,具体如下:

assert (<a.A object at 0x000001628C6878B0>, 2) == (2,)
mock_a.assert_called_once_with(mocker.ANY, nbr)