我正在使用unittest
开展一个小项目。
我可以运行我的测试但是如果我在命令行条目中添加一个参数(例如:./myprogramm.py test
而不是./myprogramm.py
),以便添加argparse
图层(argparse本身)似乎不是问题的原因)RecursionError
发生。
要重现的示例文件:
#!/usr/bin/env python3
import argparse
import unittest
def foo():
return True
class FooTestCase(unittest.TestCase):
def test_foo(self):
self.assertTrue(foo())
def test():
unittest.main()
def main():
foo()
def make_parser():
parser = argparse.ArgumentParser(description='test or foo.')
subparsers = parser.add_subparsers(dest='subcommand')
subparsers.required = True
subparsers.add_parser('test')
subparsers.add_parser('foo')
return parser
if __name__ == '__main__':
parser = make_parser()
args = parser.parse_args()
if args.subcommand == 'test':
test()
elif args.subcommand == 'foo':
main()
使用./myprogramm.py foo
启动此程序有效,但如果我使用./myprogramm.py test
启动它,则无效。
以RecursionError
输出
File "./myprogramm.py", line 14, in test
unittest.main()
File "/usr/lib/python3.5/unittest/main.py", line 93, in __init__
self.parseArgs(argv)
File "/usr/lib/python3.5/unittest/main.py", line 140, in parseArgs
self.createTests()
File "/usr/lib/python3.5/unittest/main.py", line 147, in createTests
self.module)
File "/usr/lib/python3.5/unittest/loader.py", line 219, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python3.5/unittest/loader.py", line 219, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python3.5/unittest/loader.py", line 204, in loadTestsFromName
test = obj()
File "./myprogramm.py", line 14, in test
unittest.main()
File "/usr/lib/python3.5/unittest/main.py", line 93, in __init__
self.parseArgs(argv)
File "/usr/lib/python3.5/unittest/main.py", line 114, in parseArgs
self._initArgParsers()
File "/usr/lib/python3.5/unittest/main.py", line 150, in _initArgParsers
parent_parser = self._getParentArgParser()
File "/usr/lib/python3.5/unittest/main.py", line 155, in _getParentArgParser
parser = argparse.ArgumentParser(add_help=False)
File "/usr/lib/python3.5/argparse.py", line 1629, in __init__
self._positionals = add_group(_('positional arguments'))
File "/usr/lib/python3.5/gettext.py", line 514, in gettext
return dgettext(_current_domain, message)
File "/usr/lib/python3.5/gettext.py", line 478, in dgettext
codeset=_localecodesets.get(domain))
File "/usr/lib/python3.5/gettext.py", line 413, in translation
mofiles = find(domain, localedir, languages, all=True)
File "/usr/lib/python3.5/gettext.py", line 376, in find
val = os.environ.get(envar)
File "/usr/lib/python3.5/_collections_abc.py", line 595, in get
return self[key]
RecursionError: maximum recursion depth exceeded
我怎么能(我应该?)处理这个问题?
到目前为止,为了避免这个问题,我运行del sys.argv[1]
。但是有更好的方法吗?
我猜unittest.main()
使用sys.argv
和test
作为第一个参数触发此函数的特定行为。
如果我使用其他关键字test
我得到类似的内容(在此处使用bar
):
E
======================================================================
ERROR: bar (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'bar'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)