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>A minimalistic example with no dependencies. Play with it to extend it as you wish:</p>

<pre><code>import sys
import io

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!")

def wrapper():
    lines = ["go", "bananas"]
    def fake_input(*args, **kwargs):
        return lines.pop(0)
    global input
    real_input = input
    input = fake_input
    two_answers()
    input = real_input


wrapper()
</code></pre>

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