【发布时间】:2016-03-02 06:27:13
【问题描述】:
我完全处于开发过程的中游,即将变成一个相当重要的 Python 2.7 项目。现在我将我所有的unittest 类集中在它们自己的模块tests.py 中,该模块大约有3300 行。这太疯狂了,无法导航,到处都是不好的做法,等等。
所以,我当前的任务是将其重构为子模块。作为重构的一部分,我想让从命令行运行测试的子集变得容易。例如:
$ python tests.py --all <--- runs *all* tests
$ python tests.py --utils_base <--- runs all tests on .utils
$ python tests.py --utils_vector <--- runs all tests on .utils.vector
$ python tests.py --utils_base --utils_vector <--- runs all tests on .utils.vector & .utils
所以,我开始使用argparse 进行设置。我有一个基本的ArgumentParser 设置没有问题,帮助消息显示得很好:
$ python tests.py -h
usage: tests.py [-h] [--all] [--utils_base]
optional arguments:
-h, --help show this help message and exit
Global Options:
--all Run all tests (overrides any other selections)
opan.base.utils Tests:
--utils_base Run all .utils tests
但是,当我去运行一些测试时,它崩溃了,并出现“参数无法识别”错误:
$ python tests.py --all
option --all not recognized
Usage: tests.py [options] [test] [...]
Options:
-h, --help Show this message
-v, --verbose Verbose output
-q, --quiet Minimal output
-f, --failfast Stop on first failure
-c, --catch Catch control-C and display results
-b, --buffer Buffer stdout and stderr during test runs
Examples:
tests.py - run default set of tests
tests.py MyTestSuite - run suite 'MyTestSuite'
tests.py MyTestCase.testSomething - run MyTestCase.testSomething
tests.py MyTestCase - run all 'test*' test methods
in MyTestCase
经过一番调试,我终于意识到我要在tests.py 中解析的命令行参数被保留在sys.argv 中并传递给unittest.main。 (回想起来,仔细阅读错误消息的Examples: 部分应该会更快地找到我的线索。)
所以,为了解决这个问题,我在下面添加了标记的代码,以便在将控制权传递给 unittest.main 之前从 sys.argv 中清除我的自定义参数:
# Arguments for selecting test suites
ALL = 'all'
UTILS_BASE = 'utils_base'
# Collecting the args together for iteration later
test_args = [ALL, UTILS_BASE]
...
# Strip from sys.argv any test arguments that are present
for a in test_args: <---
str_arg = '--{0}'.format(a) <--- ADDING THESE MAKES UNITTEST HAPPY
if str_arg in sys.argv: <---
sys.argv.remove(str_arg) <---
有没有办法告诉argparse 到.remove 从sys.argv 找到的参数,也许是在ArgumentParser 的构造中?我已经搜索了argparse 文档页面,但我一生都找不到合适的选项。
【问题讨论】:
标签: python python-2.7 argparse