有什么方法可以指示argparse(Python 2.7)从sys.argv中删除找到的参数? [英] Is there any way to instruct argparse (Python 2.7) to remove found arguments from sys.argv?
问题描述
对于正在变成相当庞大的Python 2.7项目的开发过程,我完全处于中间阶段.现在,我所有的 unittest
类都集中在各自的模块中,tests.py
,大约3300行.这太疯狂了,无法导航,到处都是坏习惯,等等.
I'm fully midstream in the development process for what is turning into a fairly substantial Python 2.7 project. Right now I have all of my unittest
classes lumped together in their own module, tests.py
, which stands at about 3300 lines. This is crazy big, impossible to navigate, all around bad practice, etc.
因此,我当前的任务是将其重构为子模块.作为此重构的一部分,我想简化从命令行运行一部分测试的过程.例如:
So, my current task is to refactor it into submodules. As part of this refactoring, I want to make it easy to just run a subset of the tests from the commandline. For example:
$ 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
设置,没有任何问题,并且帮助消息显示得很好:
So, I started getting things set up with argparse
. I got a basic ArgumentParser
set up without a problem, and the help message showed just fine:
$ 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
但是,当我去运行一些测试时,它崩溃并显示参数无法识别"错误:
However, when I went to run some tests, it crashed out with an 'argument not recognized' error:
$ 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:
部分应该可以使我早日明白.)
After some debugging, I finally realized that my commandline arguments to be parsed within tests.py
were being retained in sys.argv
and passed through to unittest.main
. (In retrospect, a careful read of the Examples:
section of the error message should have clued me in a lot sooner.)
因此,要解决此问题,我在下面添加了标记的代码,以便在将控制权传递给unittest.main
之前从sys.argv
清除我的自定义参数:
So, to resolve the problem, I added the marked code in the following to scrub my custom arguments from sys.argv
before passing control into unittest.main
:
# 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
告诉它从sys.argv
中找到的.remove
自变量,也许是在构造ArgumentParser
时?我已经搜索了argparse
文档页面,但是我一生中都找不到能够做到这一点的选项.
Is there any way to tell argparse
to .remove
arguments it finds from sys.argv
, perhaps in the construction of the ArgumentParser
? I've scoured the argparse
doc page, but for the life of me can't find an option that'll do it.
推荐答案
您要
尽管,我必须问.您为什么要编写自己的测试跑步者? unittest模块允许您从cli运行特定的测试集: Although, I must ask. Why are you writing your own test runner? The unittest module allows you to run specific sets of tests from the cli: 如果您在分组和运行测试方面需要更大的灵活性,建议您查看鼻子测试 If you need even more flexibility in grouping and running tests, I'd suggest looking at nosetest 这篇关于有什么方法可以指示argparse(Python 2.7)从sys.argv中删除找到的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!# run test from a spefiic class
$ python -m unittest module.tests.group.TestSpecific
# all possible options
$ python -m unittest --help
Usage: python -m unittest [options] [tests]
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:
python -m unittest test_module - run tests from test_module
python -m unittest module.TestClass - run tests from module.TestClass
python -m unittest module.Class.test_method - run specified test method
[tests] can be a list of any number of test modules, classes and test
methods.
Alternative Usage: python -m unittest discover [options]
Options:
-v, --verbose Verbose output
-f, --failfast Stop on first failure
-c, --catch Catch control-C and display results
-b, --buffer Buffer stdout and stderr during test runs
-s directory Directory to start discovery ('.' default)
-p pattern Pattern to match test files ('test*.py' default)
-t directory Top level directory of project (default to
start directory)
For test discovery all test modules must be importable from the top
level directory of the project.