How to test function, that has two or more input()'s inside?
<p>I need to test some python 3 code, and I got stuck testing function with few input()'s.</p>

<p>Example:</p>

<pre><code>def two_answers():
    if input("Input 'go' to proceed") != "go":
        return two_answers()
    else:
        while input("Input 'bananas' to proceed") != "bananas":
            print("What?!")
    print("You've just gone bananas!")
</code></pre>

<p>For functions with one input i use:</p>

<pre><code>def test_some_function(self):
    codefile.input = lambda x: 'u'
    codefile.some_function() . . . .
</code></pre>

<p>And then:</p>

<pre><code>def teardown_method(self, method):
    codefile.input = input
</code></pre>

<p>To revert inputs back.</p>

<p>But here it won't work. Help!</p>

<p>What you want is to simulate the user inputs.</p>

<p>You have to use a <a href="https://docs.python.org/3/library/unittest.mock.html" rel="nofollow"><strong>unittest.mock</strong></a> and patch the <code>input</code> function.</p>

<p>See the <a href="https://docs.python.org/3/library/unittest.mock.html#quick-guide" rel="nofollow">Quick Guide</a>.</p>

python unit-testing python-3.x python-unittest