testing interactive python programs

后端 未结 3 2286
抹茶落季
抹茶落季 2021-02-20 18:21

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_p         


        
3条回答
  •  不思量自难忘°
    2021-02-20 18:55

    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

提交回复
热议问题