Python模拟多个返回值

问题:Python模拟多个返回值

我正在使用pythons mock.patch并想更改每个调用的返回值。请注意,正在修补的函数没有输入,因此我无法根据输入更改返回值。

这是我的代码供参考。

def get_boolean_response():
    response = io.prompt('y/n').lower()
    while response not in ('y', 'n', 'yes', 'no'):
        io.echo('Not a valid input. Try again'])
        response = io.prompt('y/n').lower()

    return response in ('y', 'yes')

我的测试代码:

@mock.patch('io')
def test_get_boolean_response(self, mock_io):
    #setup
    mock_io.prompt.return_value = ['x','y']
    result = operations.get_boolean_response()

    #test
    self.assertTrue(result)
    self.assertEqual(mock_io.prompt.call_count, 2)

io.prompt仅仅是“输入”的独立于平台的版本(python 2和3)。因此,最终我将尝试模拟用户的输入。我已经尝试过使用列表作为返回值,但这并不能正常工作。

您可以看到,如果返回值无效,那么我将在此处得到一个无限循环。因此,我需要一种最终更改返回值的方法,以便测试实际上完成。

(回答此问题的另一种可能方法是解释如何在单元测试中模仿用户输入)


不是这个问题的重复,主要是因为我没有能力改变输入。

关于这个问题的答案的评论之一是相同的,但是没有提供答案/评论。

I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on the input.

Here is my code for reference.

def get_boolean_response():
    response = io.prompt('y/n').lower()
    while response not in ('y', 'n', 'yes', 'no'):
        io.echo('Not a valid input. Try again'])
        response = io.prompt('y/n').lower()

    return response in ('y', 'yes')

My Test code:

@mock.patch('io')
def test_get_boolean_response(self, mock_io):
    #setup
    mock_io.prompt.return_value = ['x','y']
    result = operations.get_boolean_response()

    #test
    self.assertTrue(result)
    self.assertEqual(mock_io.prompt.call_count, 2)

io.prompt is just a platform independent (python 2 and 3) version of “input”. So ultimately I am trying to mock out the users input. I have tried using a list for the return value, but that doesn’t seam to work.

You can see that if the return value is something invalid, I will just get an infinite loop here. So I need a way to eventually change the return value, so that my test actually finishes.

(another possible way to answer this question could be to explain how I could mimic user input in a unit-test)


Not a dup of this question mainly because I do not have the ability to vary the inputs.

One of the comments of the Answer on this question is along the same lines, but no answer/comment has been provided.


回答 0

你可以指定一个迭代side_effect,并模拟将序列中的每个被调用时,返回的下一个值:

>>> from unittest.mock import Mock
>>> m = Mock()
>>> m.side_effect = ['foo', 'bar', 'baz']
>>> m()
'foo'
>>> m()
'bar'
>>> m()
'baz'

引用Mock()文档

如果side_effect是可迭代的,则对模拟的每次调用都将返回可迭代的下一个值。

顺便说一句,测试response is not 'y' or 'n' or 'yes' or 'no'无法进行;您在问表达式(response is not 'y')是正确的还是'y'正确的(总是这样,非空字符串始终为true),等等。or运算符两侧的各种表达式都是独立的。请参阅如何针对多个值测试一个变量?

你应该不会使用is,以测试对一个字符串。CPython解释器在某些情况下可以重用字符串对象,但这不是您应该依靠的行为。

因此,请使用:

response not in ('y', 'n', 'yes', 'no')

代替; 这将使用相等性测试(==)确定是否response引用了具有相同内容(值)的字符串。

同样适用于response == 'y' or 'yes'; 使用response in ('y', 'yes')代替。

You can assign an iterable to side_effect, and the mock will return the next value in the sequence each time it is called:

>>> from unittest.mock import Mock
>>> m = Mock()
>>> m.side_effect = ['foo', 'bar', 'baz']
>>> m()
'foo'
>>> m()
'bar'
>>> m()
'baz'

Quoting the Mock() documentation:

If side_effect is an iterable then each call to the mock will return the next value from the iterable.