Python – Writing Functional and Integration Tests for Python

integration-testspython

I am new to python and functional/integration tests as a whole. I know how to write unit tests, but in this case i do not need isolation for specific functions, rather i need my python script to be run with some command line parameters and then tested for output(for example if the program is successful, a specific REST call is made).
So i need a a way to write an automated test that would treat the whole script as a black box, calling it with some arguments and watching what kind of output comes out the other end. I've heard that it's possible to do this with the standard pyunit, but i'm very unclear on how to approach running whole scripts instead of just functions in pyunit. Are there any ways to achieve what i've described? If so, could i please have a small example? Maybe there is a tool for just this kind of thing?

Best Answer

If your "whole" script is written as a set of functions including "main" function and is ending like

if __name__ == "__main__":
    main()

then all you need is actually call that main. If you have some command line parameters that are parsed, you can provide them as well.

As for REST calls - you use mocks. You either mock the actual call (if it's enough for you to just check that it was correct) or you set up a mock REST server and point your app to it.

For example, my app has an OpenID authentication. So I created my own simple OpenID server and point my application to use it as provider. That is rather slow, so I use it only when I run scenarios involving authentication. In all other cases I just patch the authentication method (I'm using tornado and I'm patching get_current_user to return a predefined user ID).

Related Topic