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>Here is my solution:</p>

<pre><code>class SimulatedInput:

    def __init__(self,*args):
        self.args = iter(args) 

    def __call__(self,x):
        try:
            return next(self.args)
        except StopIteration:
            raise Exception("No more input")
</code></pre>

<p>Then you could use it like you did before:</p>

<pre><code>def test_some_function(self):
    codefile.input = SimulatedInput("u","v")
    codefile.some_function() . . . .
</code></pre>

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