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 有没有一种方法可以计算使用一组参数调用函数mock的次数?_Python_Unit Testing_Automated Tests_Pytest - Fatal编程技术网

Python 有没有一种方法可以计算使用一组参数调用函数mock的次数?

Python 有没有一种方法可以计算使用一组参数调用函数mock的次数?,python,unit-testing,automated-tests,pytest,Python,Unit Testing,Automated Tests,Pytest,我有一个模拟api调用的函数,如下所示: def mocked_api_call(animal = animal, soundtype=soundtype) output = make_sound(animal, soundtype) return output 目标是让mock在第二次使用同一组参数调用时返回不同的输出。例如,我希望第一次调用返回“喵”,第二次调用返回“meaaowww”,第三次调用返回“喵”,如下所示: output = mocked_api_call(

我有一个模拟api调用的函数,如下所示:

def mocked_api_call(animal = animal, soundtype=soundtype)

    output = make_sound(animal, soundtype)

    return output
目标是让mock在第二次使用同一组参数调用时返回不同的输出。例如,我希望第一次调用返回“喵”,第二次调用返回“meaaowww”,第三次调用返回“喵”,如下所示:

output = mocked_api_call(animal='cat', soundtype = 'meow')
# outputs 'meow'

output = mocked_api_call(animal='cat', soundtype = 'meow')
# outputs 'MEAAAOWWW'

output = mocked_api_call(animal='cat', soundtype = 'meow')
# outputs 'mew'
这些修补程序用于测试使用调用api的函数的父函数:

def parent_function(**kwargs):
    response = make_sound(animal=animal, soundtype=soundtype)
通过monkeypatch调用测试函数,如下所示:

@mock.patch('myscript.api_call', side_effect=mocked_api_call) 
def test_parent_function(*args, **kwargs):
    output = parent_function(**kwargs)

但是我找不到一种方法来生成一个响应,这个响应取决于函数被调用的次数。这是你可以用pytest做的吗?

我不完全确定你的测试会是什么样子,但是让我们假设你想要有不同的测试,在每个测试中重置调用索引。根据您的需要,以下内容对您来说可能有点过于笼统——您也可以将索引传递到
make_sound
,如果这是您所需要的

from unittest import mock
import pytest


class TestSounds:
    indexes = {}
    sounds = {
        ('cat', 'meow'): ('meow', 'MEAAAOWWW', 'mew'),
    }

    @classmethod
    def make_sound(cls, animal, soundtype):
        key = (animal, soundtype)
        if key in cls.sounds:
            index = cls.indexes.setdefault(key, 0)
            sound = cls.sounds[key][index]
            cls.indexes[key] = (index + 1) % len(cls.sounds[key])
            return sound

    # if you need the order not be reset in each test, you can change 
    # the scope to "class"
    @pytest.fixture(scope="function", autouse=True)
    def reset_indexes(self):
        self.__class__.indexes = {}

    def test_parent_function(self, **kwargs):
        with mock.patch('myscript.api_call',
                        side_effect=[self.make_sound(**kwargs),
                                     self.make_sound(**kwargs)]):
            output1 = parent_function(**kwargs)
            output2 = parent_function(**kwargs)

请注意,这是未经测试的,但它可能会给您一些想法。

那么您希望从哪里获得输出?是否应该是随机的(例如,“meaaawww”来自哪里)?通常,您可能可以使用
副作用
,但这取决于您想要实现什么。@MrBeanBremen我希望输出不同,这取决于函数是第一次调用还是第二次调用。您看过了吗?@MrBeanBremen-我已-在上面添加了更多详细信息。我希望这能有所帮助。好吧,问题是你在一次测试中需要什么-一种特定动物和声音类型的不同输出?