I am trying to write an unit test for a function that deals with the argparsers from the users.
My function:
def __init_parser() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description='Parsing arguments...',
)
parser.add_argument('--task_id', '-t', dest='task_id', action="store", type=str, required=True)
return parser.parse_args()
Test
#pytest.mark.parametrize('task_id', ('--task_id', '-t'))
def test__init_parser_with_job_id(capsys, task_id):
args = __init_parser()
The error _jb_pytest_runner.py: error: the following arguments are required: --task_id/-t
How can I achieve this passing the number of task_id in the body of the test function?
As the comment of #hpaulj, the function looks for the argv, so in this case it is necessary to mock it and then proceed with the asserts.
def test__init_parser(monkeypatch):
task_id = '258'
with mock.patch.object(sys, 'argv', ['startup.py', '--task_id', '258'):
args = __init_parser()
job_id = getattr(args, 'task_id')
assert task_id == '258'
Related
How do I use argparse to send selective arguments to other scripts. The scripts invoked are imported as modules and the folder structure is as below:
Directory Structure - hello.py
- cloud_module
- script1
- script2
In hello.py script, I am trying to invoke scripts based on argument conditions and pass selective remainder arguments -
hello.py
from cloud_module import script1,script2
import argparse
def parse_arguments(parser):
parser.add_argument('--name', type=str, required=True)
parser.add_argument('--cloud', type=str, required=True)
parser.add_argument('--service', type=str, required=True)
parser.add_argument('--zone', type=str, required=True)
parser.add_argument('--billing', type=str, required=True)
def parse_command_line_arguments():
parser = argparse.ArgumentParser()
parse_arguments(parser)
args = parser.parse_args()
arguments = args.__dict__
return args
def output(args):
if args.name == 'script1':
**// Pass values to script1.py: cloud & service**
elif args.name == 'script2':
**// Pass values to script2.py: zone & billing**
if __name__ == "__main__":
arguments = parse_command_line_arguments()
output(arguments)
script1.py
import argparse
def parse_arguments(parser):
parser.add_argument('--cloud', type=str, required=True)
parser.add_argument('--service', type=str, required=True)
def parse_command_line_arguments():
parser = argparse.ArgumentParser()
parse_arguments(parser)
args = parser.parse_args()
arguments = args.__dict__
return args
def func1(arguments):
print('this is script1')
if __name__ == "__main__":
arguments = parse_command_line_arguments()
func1(arguments)
You can use subparsers for parsing different scripts with different arguments. Here is a simple example for the same
subparser_example.py
import argparse
def script_1_run(a, b):
print( f'Running script 1 with {a}, {b}')
def script_2_run(x, y):
print( f'Running script 2 with {x}, {y}')
def add_parser_for_script1( parser: argparse.ArgumentParser ):
parser.add_argument('-a', '--argument_a', type=str, dest='a')
parser.add_argument('-b', '--argument_b', type=str, dest='b')
def add_parser_for_script2( parser: argparse.ArgumentParser ):
parser.add_argument('-x', '--argument_y', type=str, dest='x')
parser.add_argument('-y', '--argument_x', type=str, dest='y')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(title = 'Script select', dest='script_type')
subparser.required = True
add_parser_for_script1(subparser.add_parser('script1'))
add_parser_for_script2(subparser.add_parser('script2'))
args = parser.parse_args()
if args.script_type == 'script1':
script_1_run(args.a, args.b)
elif args.script_type == 'script2':
script_2_run(args.x, args.y)
Usage:
for script 1
py subparser_example.py script1 -a argument_a -b argument_b
for script 2
py subparser_example.py script2 -x argument_x -y argument_y
Is it possible to use Argparse from within a class and if so, how would you get the parameters to the parser? Is there a better way of going about this? Here is my code so far:
class Game:
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument("num_players", help="Number of players", type=int)
...
args = parser.parse_args()
if __name__ == '__main__':
g = Game()
Also Is there a way of supplying optional arguments such as --verbose?
It is highly unlikely to be the best design.
The Game class should probably not care how it was initialized, and it should probably know nothing about command line arguments. You will be better off parsing the CLI args outside, then pass the arguments to Game.__init__ or at least to a dedicated Game.from_cli_args classmethod.
import argparse
class Game:
def __init__(self, num_players):
print('Game got {} as num_players'.format(num_players))
self.num_players = num_players
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("num_players", help="Number of players", type=int)
...
args = parser.parse_args()
g = Game(args.num_players)
Then executing:
$ python main.py 2
Game got 2 as num_players
If you use -- as a prefix the argument will need to be passed explicitly:
parser.add_argument("--num_players", help="Number of players", type=int)
Then
$ python main.py --num_players 2
Game got 2 as num_players
yeah, you can use argparse inside class constructor.just create parser object and pass all arguments to that object, once you initialise class you can access all arguments using that parser object.
class test:
def __init__(self):
self.parser = argparse.ArgumentParser(description='Process some integers.')
self.parser.add_argument('integers', metavar='N', type=int, nargs='+',help='an integer for the accumulator')
def test123(self):
args = self.parser.parse_args()
# prints argument
print(args)
if __name__ == '__main__':
x = test()
x.test123()
output: Namespace(integers=[1, 2, 3, 4])
I'd like to pass my own arguments into files that are setup for unittest. So calling it from the command line like this should work:
python Test.py --c keith.ini SomeTests.test_one
Currently I'm running into two issues.
1) Arg parse doesn't allow unknown arguments
usage: Test.py [-h] [--c CONFILE]
Test.py: error: unrecognized arguments: SomeTests.test_one
2) Unit test doesn't allow unknown arguments. So --c fileName is not accepted by unittest and returns:
AttributeError: 'module' object has no attribute 'keith'
So the idea is to collect my arguments and remove them before calling unittest runner.
import unittest
import argparse
myArgs = None
def getArgs( allArgs ):
parser = argparse.ArgumentParser( )
parser.add_argument('--c', dest='conFile', type=str, default=None, help='Config file')
args = parser.parse_args()
if ( args.conFile == None ):
parser.print_help()
return args
class SomeTests(unittest.TestCase):
def test_one(self):
theTest( 'keith' )
def test_two(self):
otherTest( 'keith' )
if __name__ == '__main__':
myArgs = getArgs( sys.argv )
print 'Config File: ' + myArgs.conFile
unittest.main( argv=sys.argv, testRunner = unittest.TextTestRunner(verbosity=2))
Interesting I just found parse_known_args() so I changed the parse line to:
args = parser.parse_known_args(['--c']).
I thought this would solve my issue and give me something to pass to unittest. Unfortunately I get:
Test.py: error: argument --c: expected one argument.
Shouldn't this work?
OK took a bit of effort but figured it out. This is totally possible. The documentation for argparse is not correct. The function parse_known_args() should not include a list of known arguments. Also argparse removes arg[0] which is important to return so other commands see a valid argument list. I'd consider this removal a bug. I have included the final example code.
import unittest
import argparse
import sys
myArgs = None
def getArgs( allArgs ):
parser = argparse.ArgumentParser( )
parser.add_argument('--c', dest='conFile', type=str, default=None, help='Configuration file. (Required)')
args, addArgs = parser.parse_known_args( )
if ( args.conFile == None ):
parser.print_help()
sys.exit(2)
# argparse strips argv[0] so prepend it
return args, [ sys.argv[0]] + addArgs
def verify( expected, actual ):
assert expected == actual, 'Test Failed: '
# Reusable Test
def theTest( exp ):
print 'myargs: ' + str( myArgs )
verify( exp, 'keith' )
def otherTest( exp ):
theTest( exp )
class SomeTests(unittest.TestCase):
def test_one(self):
theTest( 'keith' )
def test_two(self):
otherTest( 'keith2' )
if __name__ == '__main__':
myArgs, addArgs = getArgs( sys.argv )
unittest.main( argv=addArgs, testRunner = unittest.TextTestRunner(verbosity=2))
Once you save this to a file you can call it like the examples below and it will all work.
python Test.py # Requires config file
python Test.py --c keith.ini # Runs all tests
python Test.py --c keith.ini SomeTests # Runs Class
python Test.py --c keith.ini SomeTests.test_one # Runs test
HTH, Enjoy
I am struggling to write a simple python script (a pseudo-git), which will allow me to call it from commandline/shell using comand like this:
$ python script.py init
I found some solutions online, which enabled me to do so, but there is little issue though. I want "add" function to accept other arguments as well (strings in general, which will represent files' names).
I have found a workaround, but it doesn't look nice. Is there a way to refactor the code, so that "add" will accept also other arguments and let me access them later on? Important thing: I don't want them to be added with "--", I'd rather separate them by simply adding space between two arguments.
I found "nargs='+' option in add_argument, but I don't know how to redirect the argument to call a proper function.
Here is my code I wrote so far:
import argparse
import sys
def init():
print("init method call")
def add():
if(len(sys.argv)>2):
print("valid add method call")
else:
print("invalid call")
def commit():
print("commit method call")
def status():
print("status method call")
def test():
print("test method call")
FUNCTION_MAP = {'init' : init,
'status' : status,
'commit': commit}
if __name__ == '__main__':
if(len(sys.argv)>1 and sys.argv[1] == "add"):
add()
else:
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=FUNCTION_MAP.keys())
args = parser.parse_args()
func = FUNCTION_MAP[args.command]
func()
Here are 2 options - take an extra positional, and use or ignore the values. Or use subparsers.
import argparse
def add(args):
print("add:", args)
def status(args):
values = getattr(args,'values',None)
if values and len(values)>0:
# error message and method of your choice
print('oops - got values', args)
print("status method call")
FUNCTION_MAP = {'add' : add,
'status' : status}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=FUNCTION_MAP.keys())
parser.add_argument('values', nargs='*')
args = parser.parse_args()
func = FUNCTION_MAP[args.command]
func(args)
print('\nWIth subparsers')
parser = argparse.ArgumentParser()
sp = parser.add_subparsers(dest='command')
spp = sp.add_parser('add')
spp.add_argument('values', nargs='*')
spp = sp.add_parser('status')
args = parser.parse_args()
func = FUNCTION_MAP[args.command]
func(args)
Sample runs
1906:~/mypy$ python3 stack49691897.py add 1 2 3
add: Namespace(command='add', values=['1', '2', '3'])
WIth subparsers
add: Namespace(command='add', values=['1', '2', '3'])
1907:~/mypy$ python3 stack49691897.py status
status method call
WIth subparsers
status method call
1907:~/mypy$ python3 stack49691897.py status 1 2 3
oops - got values Namespace(command='status', values=['1', '2', '3'])
status method call
WIth subparsers
usage: stack49691897.py [-h] {add,status} ...
stack49691897.py: error: unrecognized arguments: 1 2 3
The argparse docs also demonstrates a way of using subparsers and defaults that effectively implements your FUNCTION_MAP.
So I have a script in which my argument parsing functions are separated for a cleaner design. Ultimately, I wish to run a single command and have all the arguments parsed by those 3 functions. The command would look like:
python3 rhize_refactored.py -l <str>, -sa, [-cr], -si <int>, -i <input_path>, -o <output_path>, [-r], [-c]
In order for all arguments to be recognized, I've set up the script so that any extra arguments ignored by the first argument parsing function get passed on to the second argument parsing function, and again with the third argument parsing function. That part looks like this:
#Argument parsing functions#
def parse_args_language():
parser=ArgumentParser(prog= 'rhize.py')
parser.add_argument('-l', dest='language', choices= ['bash', 'python'], type=str, default='bash') #required
args, extras1= parser.parse_known_args() #pass extras down to parse_args_bash()
return args
return extras1
def parse_args_bash(extras1):
parser=ArgumentParser()
parser=parser.add_argument('-sa', action='store_true') #required
parser=parser.add_argument('-cr', action='store_true') #optional
parser=parser.add_argument('-si', type=int) #required
parser=parser.add_argument('-i') #required
parser=parser.add_argument('-o') #required
args=parser.parse_args(argv =extras1)
extras2= parser.parse_known_args() #pass extras down to parse_args_repo
return args
return extras2
def parse_args_repo(extras2):
parser= ArgumentParser()
parser.add_argument('-r', action= 'store_true') #optional
parser.add_argument('-c', action= 'store_true') #optional
args=parser.parser_args(argv=extras2)
return args
##############################################################
def rhize_bash():
args, extras1= parse_args_language()
parse_args_bash(extras1)
make_templates()
....
def make_templates():
args, extras2= parse_args_bash()
parse_args_repo(extras2)
...
def main():
language= parse_args_language()
if language == "bash":
rhize_bash()
if language == "python":
rhize_python() #omitted from this post
print("Completed the run.")
main()
Have I set this up the right way? Because when I try running the full script, it appears to run through it fully, even though I know it shouldn't.
Here's an attempt to make the code flow correctly. I haven't tested it.
#Argument parsing functions#
def parse_args_language():
parser=ArgumentParser(prog= 'rhize.py')
parser.add_argument('-l', dest='language', choices= ['bash', 'python'], default='bash')
args, extras1 = parser.parse_known_args() #pass extras down to parse_args_bash()
return args, extras1 # return a tuple of items
def parse_args_bash(extras1):
parser=ArgumentParser()
parser=parser.add_argument('--sa', action='store_true')
# store_true actions are always optional
parser=parser.add_argument('--cr', action='store_true')
parser=parser.add_argument('--si', type=int)
parser=parser.add_argument('-i')
parser=parser.add_argument('-o')
args, extras2= parser.parse_known_args() #pass extras down to parse_args_repo
return args, extras2
def parse_args_repo(extras2):
parser= ArgumentParser()
parser.add_argument('-r', action= 'store_true')
parser.add_argument('-c', action= 'store_true')
args=parser.parser_args(argv=extras2)
return args
##############################################################
def rhize_bash(extras1):
args1, extras2 = parse_args_bash(extras1)
make_templates(extras2)
....
def make_templates(extras2):
args2 = parse_args_repo(extras2)
...
def main():
args, extras1 = parse_args_language()
if args.language == "bash":
rhize_bash(extras1)
elif args.language == "python":
rhize_python(extras1) #omitted from this post
print("Completed the run.")
if __name__ == "__main__":
main()
The parse_args_bash and parse_args_repo return separate args namespace objects. We could pass the args to parse_args_repo, and have it add its values to that. But I'll skip that step for now.
Your code called parse_args_language a couple of times, once to get the args.language value, and once to get extras1 to pass to on. No harm in doing that, but rewrote it so it is called just once.
Since it is using parse_known_args, parse_args_bash should work using the default sys.argv, since it would just ignore the -l argument. But it's also ok to work with extras which strips that out.