python, unittest: is there a way to pass command line options to the app

Viewed 45856

I have a module that imports unittest and has some TestCases. I would like to accept some command-line options (for example below, the name of a data file), but when I try to pass the option I get the message option -i not recognized. Is it possible to have unittest + provide options to the app (note: I'm using optparse to handle the options)? Thanks.

$ python test_app_data.py -i data_1.txt

option -i not recognized

=====================

follow-up: this is an implementation of the suggested solution:

import cfg_master  #has the optparse option-handling code

...

if __name__ == '__main__':    
    #add you app's options here...
    options_tpl = ('-i', '--in_dir', '-o', '--out_dir')
    del_lst = []
    for i,option in enumerate(sys.argv):
        if option in options_tpl:
            del_lst.append(i)
            del_lst.append(i+1)

    del_lst.reverse()
    for i in del_lst:
        del sys.argv[i]
        
    unittest.main()
6 Answers

In your if __name__ == '__main__': section, which you're not showing us, you'll need to optparse and then del sys.argv[1:] before you pass control to unittest code, so that the latter code doesn't try to interpret your command line options again when you've already dealt with them. (It's a bit harder to have some options of your own and also pass some down to unittest, though it can be done if you do have such complex needs).

In addition to the other answers I'd like to provide a different view on the question. I know the question asked for a way to provide command line options to unit tests, however the actual problem that is stated is just "how can I pass parameters to unit tests".

This can also be achieved with environment variables.

Here's an example how you could provide different data to a test locally and on a build server:

my-script.py:

from os import environ

database = MyDatabase(
    user = 'root',
    password = 'some-pw',
    host = environ['MYSQL_HOST'],
    port = environ['MYSQL_TCP_PORT']
)

test-local.sh:

MYSQL_TCP_PORT="3311"
MYSQL_HOST="localhost"
python3.7 -m unittest

test-ci.sh:

MYSQL_TCP_PORT="3306"
MYSQL_HOST="my-database"
python3.7 -m unittest

You should not take arguments and options to run unittests, as you make them run under different, less predictable conditions this way. You should figure out why you need to run tests with different data, and make you test suite complete enough to cover the ground of all data sets without being run differently each time.

Related