python 带有传递参数的单元测试 -b
python unittest with passed arguments -b
快速总结:让 python 中的单元测试工作,接受 cmd 行参数并使用 -b 标志进行 运行 测试
我在名为的文件中有以下代码: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
非常简单,工作正常。我接受命令行内容并将其塞入字典。现在我想添加一个非常基本的单元测试(我已经有很多正在工作)使用:
import unittest
在一个名为 test_filetools.py 的文件中,这有效:
def test_get_passedargdict(self):
passedargdict = get_passedargdict()
self.assertFalse(passedargdict["keepinput"])
self.assertFalse(passedargdict["keepconfig"])
self.assertFalse(passedargdict["skipmd5"])
我可以运行:
test_filetools.py
而且效果很好。我热爱生活。但是我这里有很多单元测试,我不想要屏幕上的所有垃圾邮件我只想要小点,所以我尝试了:
test_filetools.py -b
我失败了:
Stderr:
usage: test_filetools.py [-h] [-skipmd5 SKIPMD5] [-keepinput KEEPINPUT]
[-keepconfig KEEPCONFIG]
test_filetools.py: error: unrecognized arguments: -b
所以看起来我的 get_passedargdict() 正试图引入 -b。 如果我把那个单元测试出来,那么所有 运行 都可以使用 -b。
有什么想法可以使这个单元测试接受参数并能够同时使用 -b 选项而不会导致此失败?谢谢!
看来我自己的 post 有点像骗子所以我会自己回答!这个post看起来有很多选择:
How do you write tests for the argparse portion of a python module?
我让函数接受参数,在实际代码中将命令行内容传递给它,在测试硬代码中它是空的。
这是我为测试所做的更改:
def test_get_passedargdict(self):
passedargdict = get_passedargdict([])
self.assertFalse(passedargdict["keepinput"])
self.assertFalse(passedargdict["keepconfig"])
self.assertFalse(passedargdict["skipmd5"])
我现在的实际代码是这样的:
passedargdict = get_passedargdict(sys.argv[1:])
和函数:
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
快速总结:让 python 中的单元测试工作,接受 cmd 行参数并使用 -b 标志进行 运行 测试
我在名为的文件中有以下代码: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
非常简单,工作正常。我接受命令行内容并将其塞入字典。现在我想添加一个非常基本的单元测试(我已经有很多正在工作)使用:
import unittest
在一个名为 test_filetools.py 的文件中,这有效:
def test_get_passedargdict(self):
passedargdict = get_passedargdict()
self.assertFalse(passedargdict["keepinput"])
self.assertFalse(passedargdict["keepconfig"])
self.assertFalse(passedargdict["skipmd5"])
我可以运行:
test_filetools.py
而且效果很好。我热爱生活。但是我这里有很多单元测试,我不想要屏幕上的所有垃圾邮件我只想要小点,所以我尝试了:
test_filetools.py -b
我失败了:
Stderr:
usage: test_filetools.py [-h] [-skipmd5 SKIPMD5] [-keepinput KEEPINPUT]
[-keepconfig KEEPCONFIG]
test_filetools.py: error: unrecognized arguments: -b
所以看起来我的 get_passedargdict() 正试图引入 -b。 如果我把那个单元测试出来,那么所有 运行 都可以使用 -b。
有什么想法可以使这个单元测试接受参数并能够同时使用 -b 选项而不会导致此失败?谢谢!
看来我自己的 post 有点像骗子所以我会自己回答!这个post看起来有很多选择:
How do you write tests for the argparse portion of a python module?
我让函数接受参数,在实际代码中将命令行内容传递给它,在测试硬代码中它是空的。
这是我为测试所做的更改:
def test_get_passedargdict(self):
passedargdict = get_passedargdict([])
self.assertFalse(passedargdict["keepinput"])
self.assertFalse(passedargdict["keepconfig"])
self.assertFalse(passedargdict["skipmd5"])
我现在的实际代码是这样的:
passedargdict = get_passedargdict(sys.argv[1:])
和函数:
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