sniperd
sniperd

Reputation: 5274

python unittest with passed arguments -b

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!

Upvotes: 0

Views: 1262

Answers (1)

sniperd
sniperd

Reputation: 5274

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

Upvotes: 1

Related Questions