0

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?

1 Answer 1

1

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).

2
  • Okay, thank you for clearing that up, but how specifically would i pass it command line parameters? codepaste.net/hd3z99 ? And how could i listen for REST calls?
    – Reinis
    Sep 19, 2014 at 6:00
  • You are probably using some kind of parser to parse arguments and feed them to main. Now you just call main with required parameters. I've updated my answer about REST calls.
    – aragaer
    Sep 19, 2014 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.