Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set argparse arguments from python script

I have a main function specified as entry point in my package's setup.py which uses the argparse package in order to pass command line arguments (see discussion here):

# file with main routine specified as entry point in setup.py
import argparse
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('a', type=str, help='mandatory argument a')
    args = parser.parse_args()

Ideally, I would like to use the same main function in the package's tests as suggested here. In the latter context, I would like to call the main function from within the test class and set (some of) the command line arguments prior to the function call (which otherwise will fail, due to missing arguments).

# file in the tests folder calling the above main function
class TestConsole(TestCase):
    def test_basic(self):
        set_value_of_a()
        main()

Is that possible?

like image 414
XabiFermal Avatar asked Oct 04 '16 13:10

XabiFermal


1 Answers

The argparse module actually reads input variables from special variable, which is called ARGV (short from ARGument Vector). This variable is usually accessed by reading sys.argv from sys module.

This variable is a ordinary list, so you can append your command-line parameters to it like this:

import sys
sys.argv.extend(['-a', SOME_VALUE])
main()

However, messing with sys.argv at runtime is not a good way of testing. A much more cleaner way to replace the sys.argv for some limited scope is using unittest.mock.patch context manager, like this:

with unittest.mock.patch('sys.argv'. ['-a', SOME_VALUE]):
    main()

Read more about unittest.mock.patch in documentation

Also, check this SO question:

How do I set sys.argv so I can unit test it?

like image 72
Evgeny Sharypin Avatar answered Oct 06 '22 14:10

Evgeny Sharypin