2

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 (i.e. it requires two inputs in sequence). This is my attempt - the with statement doesn't advance the iterator, but I don't know how I would implement patched input otherwise.

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
2
  • This input/answers are sequentials? Commented Apr 6, 2021 at 18:17
  • strings - first "y" then "n" Commented Apr 6, 2021 at 18:23

1 Answer 1

2

If I understood well the problem, you have a fucntion that have a similar behavior like this.

module.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. You were in the right way, the only point to fix is that you have to build 2 mocks. One for each input instance.

test_module.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')

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.