[英]mock patch multiple user inputs in sequence
similar questions have been asked many times, but I can't seem to figure out this simple test I am trying to build: I would like to first supply a "y", and then a "n" to a complex function requiring user input (ie it requires two inputs in sequence).类似的问题已被问过很多次,但我似乎无法弄清楚我正在尝试构建的这个简单测试:我想先提供一个“y”,然后为需要用户输入的复杂 function 提供一个“n” (即它需要按顺序输入两个输入)。 This is my attempt - the with statement doesn't advance the iterator, but I don't know how I would implement patched input otherwise.
这是我的尝试 - with 语句不会推进迭代器,但我不知道我将如何实现修补输入。
import mock
m = mock.Mock()
m.side_effect = ["y","n"]
@pytest.fixture(scope="module")
def test_my_complex_function():
with mock.patch('builtins.input', return_value=m()):
out = my_complex_function(some_args)
return out
If I understood well the problem, you have a fucntion that have a similar behavior like this.如果我很好地理解了这个问题,那么您就有一个具有类似行为的功能。
module.py模块.py
def complex_function():
first = input("First input")
second = input("Second input")
return first, second
And you would to like to mock the input
builtin method.并且您想模拟
input
内置方法。 You were in the right way, the only point to fix is that you have to build 2 mocks.你的方法是对的,唯一要解决的问题是你必须构建 2 个模拟。 One for each
input
instance.每个
input
实例一个。
test_module.py测试模块.py
import pytest
from unittest.mock import Mock, patch
from module import complex_function
input_mock_y = Mock() # First mock for first input call
input_mock_n = Mock() # Second mock for second input call
input_mock = Mock() # Combine the 2 mocks in another mock to patch the input call.
input_mock.side_effect = [input_mock_y.return_value, input_mock_n.return_value]
def test_my_complex_function():
with patch('builtins.input', input_mock) as mock_input:
result = complex_function()
assert mock_method.call_count == 2
You may say: Ok, but how do I know that each input was patched correctly?你可能会说:好的,但是我怎么知道每个输入都被正确地修补了呢? So, you can especify some return value to any input mock, so you can compare.
因此,您可以为任何输入模拟指定一些返回值,以便进行比较。
input_mock_y = Mock()
input_mock_y.return_value = "Y"
input_mock_n = Mock()
input_mock_n.return_value = "N"
input_mock = Mock()
input_mock.side_effect = [input_mock_y.return_value, input_mock_n.return_value]
def test_my_complex_function():
with patch('builtins.input', input_mock) as mock_method:
result = function()
assert mock_method.call_count == 2
assert result == ('Y', 'N')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.