7

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.

3
  • 2
    Why do you think you need to test the command-line interface of the module? Testing optparse is not your job, and there's little you can do wrong when specifying your flags. Besides, if you run the whole application from entry point to exit value, it's not a unit test. Commented Apr 12, 2012 at 22:44
  • @delnan Thanks for the reply. I'm sorry I don't quite follow. I'm not trying to test optparse, I just want to know how to pass specific flag values to the module from within unittest, so it effectively tests the module as if it was being called from the command line. The module needs the command line values to operate. Commented Apr 12, 2012 at 22:57
  • If you want to test your code, test your code and not the glue code supplying it with command line arguments - it's easier and gives you more information. For instance, if you have two functions that play a role in computing the result, test each of them individually. Or at least stop calling it a unit test. Commented Apr 13, 2012 at 10:22

2 Answers 2

7

You just have to modify the list sys.argv which represents the list of command line arguments. This list is global to the python interpreter so you don't have to pass it around.

import sys
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-e")
parser.add_option("-g")

print sys.argv # unmodified
(options, args) = parser.parse_args()
print (options, args)

sys.argv = ['module.py','-e','42','-g','84'] # define your commandline arguments here

(options, args) = parser.parse_args()
print (options, args)

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mash, that is the answer I was after. I was unaware sys.argv was a modifiable function to implement commandline arguments. So just to clarify, sys.argv is the global function able to store multiple commandline inputs relating to multiple modules, if I so desire?
1

Three alternatives:

1) Use some shell, instead of fixating on 100% Python.

2) Pass around sys.argv (and perhaps a workalike vector) as needed

3) Call the functions that your command line option dispatcher calls instead

1 Comment

Thanks for the reply. Is it possible to use shell from with in a Python unittest? Sorry reasonably new to Python so am unaware of that. I'd rather keep all the code within the unittest. I have no idea how to pass around sys.argv, but will read up on it. optparse is called and defined in module.py's init, which is called when the module in instantiated. Is that what you mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.