I am writing a unittest for a module that accepts command line arguments. I’ve used optparse in the module to accept the args.
So when I execute the module directly I just type:-
module.py -e 42 -g 84
So far in my unittest I simply create an instance of the module to test then call a specific method:-
instance = module.className()
instance.method()
Can someone please tell me how to pass the command line args to module.py from another module such as a unittest?
Do I use optparse in my unittest and somehow incorporate in when generating the instance of module.py?
Thanks in advance.
You just have to modify the list
sys.argvwhich represents the list of command line arguments. This list is global to the python interpreter so you don’t have to pass it around.Note: The OptionParser Module is deprecated so if possible switch to
argparse(see: http://docs.python.org/library/argparse.html#module-argparse). All you need is Python 2.7.