[TIP] argv with unitest

Benji York benji at benjiyork.com
Tue Aug 18 05:43:18 PDT 2009

On Tue, Aug 18, 2009 at 7:29 AM, Mag Gam<magawake at gmail.com> wrote:
> How do I simulate argv with unittest? Is it better to place it in
> setUp() or somewhere else?

The way I normally do that is to do all my argv processing in a main
function which expects argv to be passed in.  Like so:

def main(argv):
    # parse argv
    return exit_code

if __name__ == '__main__':

With the above you can have your tests call main with whatever values
you want without munging sys.argv and also test that it returns the
right exit codes without catching SystemExit.
Benji York

More information about the testing-in-python mailing list