0

quick summary: getting a unittest in python to work that takes in cmd line arguments and using the -b flag for running the test


I have the following code in a file called: filetool.py

def get_passedargdict():
 '''need a place to setup args'''
 parser = argparse.ArgumentParser(description="pass me some data")
 parser.add_argument("-skipmd5", "--skipmd5", help="skip the md5 check", required=False)
 parser.add_argument("-keepinput", "--keepinput", help="copy instead of moving the input file", required=False)
 parser.add_argument("-keepconfig", "--keepconfig", help="copy instead of moving the config file", required=False)
 passedargs = parser.parse_args()
 passedargdict = {}
 if passedargs.skipmd5 == "y":
 passedargdict["skipmd5"] = True
 else:
 passedargdict["skipmd5"] = False
 if passedargs.keepinput == "y":
 passedargdict["keepinput"] = True
 else:
 passedargdict["keepinput"] = False
 if passedargs.keepconfig == "y":
 passedargdict["keepconfig"] = True
 else:
 passedargdict["keepconfig"] = False
 return passedargdict

Pretty straight forward, works fine. I take in command line stuff and stuff it into a dictionary. Now I want to add a very basic unit test (I have many already that are working) using:

import unittest

in a file called test_filetools.py and this works:

def test_get_passedargdict(self):
 passedargdict = get_passedargdict()
 self.assertFalse(passedargdict["keepinput"])
 self.assertFalse(passedargdict["keepconfig"])
 self.assertFalse(passedargdict["skipmd5"])

And I can run:

test_filetools.py

And it works great. I'm loving life. But I have a lot of unittests here and I don't want all the spam on the screen I just want the little dots, so I tried:

test_filetools.py -b

And I get a failure:

Stderr:
usage: test_filetools.py [-h] [-skipmd5 SKIPMD5] [-keepinput KEEPINPUT]
 [-keepconfig KEEPCONFIG]
test_filetools.py: error: unrecognized arguments: -b

So it looks like my get_passedargdict() is trying to pull in the -b. If I take that unit test out it all runs fine with -b.

Any ideas how I can have this unittest that takes in arguments and be able to use the -b option at the same time without causing this failure? Thank you!

asked Oct 9, 2017 at 14:34

1 Answer 1

1

Looks like my own post is sort of a dupe so I'll answer it myself! This post looks to have a lot of options:

How do you write tests for the argparse portion of a python module?

I went with make the function take the args, and in the real code pass the command line stuff to it, in the test hard code it empty.

And here is what I changed for my test:

def test_get_passedargdict(self):
 passedargdict = get_passedargdict([])
 self.assertFalse(passedargdict["keepinput"])
 self.assertFalse(passedargdict["keepconfig"])
 self.assertFalse(passedargdict["skipmd5"])

and my actual code is like this now:

passedargdict = get_passedargdict(sys.argv[1:])

and the function:

def get_passedargdict(args):
 '''need a place to setup args'''
 parser = argparse.ArgumentParser(description="pass me some data")
 parser.add_argument("-skipmd5", "--skipmd5", help="skip the md5 check", required=False)
 parser.add_argument("-keepinput", "--keepinput", help="copy instead of moving the input file", required=False)
 parser.add_argument("-keepconfig", "--keepconfig", help="copy instead of moving the config file", required=False)
 passedargs = parser.parse_args(args)
 passedargdict = {}
 if passedargs.skipmd5 == "y":
 passedargdict["skipmd5"] = True
 else:
 passedargdict["skipmd5"] = False
 if passedargs.keepinput == "y":
 passedargdict["keepinput"] = True
 else:
 passedargdict["keepinput"] = False
 if passedargs.keepconfig == "y":
 passedargdict["keepconfig"] = True
 else:
 passedargdict["keepconfig"] = False
 return passedargdict
answered Oct 10, 2017 at 17:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.