testing interactive python programs
Question: I would like to know which testing tools for python support the testing of
interactive programs. For example, I have an application launched by:

    
    
    $ python dummy_program.py 
    
    >> Hi whats your name? Joseph
    

I would like to instrument `Joseph` so I can emulate that interactive
behaviour.


Answer: Your best bet is probably dependency injection, so that what you'd ordinarily
pick up from sys.stdin (for example) is actually an object passed in. So you
might do something like this:

    
    
    import sys
    
    def myapp(stdin, stdout):
        print >> stdout, "Hi, what's your name?"
        name = stdin.readline()
        print >> stdout "Hi,", name
    
    # This might be in a separate test module
    def test_myapp():
        mock_stdin = [create mock object that has .readline() method]
        mock_stdout = [create mock object that has .write() method]
        myapp(mock_stdin, mock_stdout)
    
    if __name__ == '__main__':
        myapp(sys.stdin, sys.stdout)
    

Fortunately, Python makes this pretty easy. Here's a more detailed link for an
example of mocking stdin: <http://konryd.blogspot.com/2010/05/mockity-mock-
mock-some-love-for-mock.html>

