- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我完全处于开发过程的中游,正在转变为一个相当重要的 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
文档页面,但我终究找不到可以做到这一点的选项。
最佳答案
from __future__ import print_function
import argparse
import sys
def main():
print(sys.argv)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--all', action='store_true')
parser.add_argument('--utils_base', action='store_true')
args, left = parser.parse_known_args()
sys.argv = sys.argv[:1]+left
main()
虽然,我必须问。你为什么要编写自己的测试运行程序? unittest 模块允许您从 cli 运行特定的测试集:
# 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.
如果您在分组和运行测试方面需要更大的灵 active ,我建议您查看 nosetest
关于python - 有什么方法可以指示 argparse (Python 2.7) 从 sys.argv 中删除找到的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733262/
我正在尝试使用 parser = argparse.ArgumentParser对于我写的一个小程序。 程序接受作为输入 EITHER ( txt 文件的路径 ) OR ( opt1 && opt2
我为需要遵循特定格式的参数定义了自定义正则表达式类型。我使用了另一篇文章 ( regex custom type ) 中的代码,它非常有用。我的问题是我正在编写单元测试,我希望正则表达式失败并尝试断言
我写了下面的代码。 import argparse parser = argparse.ArgumentParser() parser.add_argument('-v', '--version',
我一定遗漏了一些明显的东西。目标是使用 argparse,第一个参数是必需的,第二个是可选的,其他任何剩余参数都是可选的。 为了展示这个问题,我制作了两个测试解析器;它们之间的唯一区别是在一个中使用
我正在努力寻找一种将参数传递给 docker container 中的 python 脚本的方法。基于ubuntu 。我正在与docker-compose.yml合作. 请查看下面的示例! docke
我正在努力寻找一种将参数传递给 docker container 中的 python 脚本的方法。基于ubuntu 。我正在与docker-compose.yml合作. 请查看下面的示例! docke
我像这样创建一个 argparser: parser = argparse.ArgumentParser(description='someDesc') parser.add_argument
我正在编写一个脚本,其中包含 2 个相互排斥的参数,以及一个仅对其中一个参数有意义的选项。如果您使用毫无意义的参数调用它,我会尝试将 argparse 设置为失败。 要清楚: -m -f 有意义 -s
我正在使用 Python 3.6 和 argparse 1.1。 除了 -h/--help 我还想有一个选项 -v/--version打印带有版本信息的字符串并退出程序(就像使用帮助字符串一样)。但是
有没有办法将来自父解析器的参数分组到不同的组中?我无权访问父解析器本身,所以我不能在那里添加组。 (我使用的是 Google 的 OAuth2 框架)。 目前我的代码是: # test.py from
GNU grep 有一个参数可以在匹配行周围打印一定数量的额外行。从手册页: -C NUM, -NUM, --context=NUM Print NUM lines of output context
现在,我有一个脚本可以使用 argparse 接受命令行参数。例如,像这样: #foo.py def function_with_args(optional_args=None): parser
我在脚本中的 argparse 解析器中添加了一些参数,如下所示: parser = argparse.ArgumentParser() parser.add_argument("--eval_mod
我在 python 代码中使用 argparse 作为参数解析器。将字典解析为 argparse 对象的最佳方法是什么? 例如,我的字典是: { "activation_dropout": 0
我正在使用 argparse从命令行接收输入以运行我的脚本。 我当前的输入字符串如下所示: path> python -t T1 T2 T3 -f F1 F2 argparse 中是否有一个参数,而
我看到了有关使用 argparse 库将字典和列表传递给 Python 的问题。这些示例都显示了我的 Python 代码的样子。但是没有人告诉我它们在命令行上的样子。我在哪里需要大括号、方括号和引号?
我想模拟大多数命令行实用程序的行为,其中可选参数可以放在命令行中的任何位置,包括位置参数之间,例如 mkdir例子: mkdir before --mode 077 after 在这种情况下,我们知道
我想获取传递给 sys.argv 的所有参数有格式someprogram.py --someparameter 23 -p 42 -anotherparam somevalue . 结果我正在寻找一个
这个问题在这里已经有了答案: Argparse optional positional arguments? (3 个回答) 1年前关闭。 我如何才能设置我的 argparser 以具有以下行为? b
这是我的简单 test.py 脚本: import argparse parser = argparse.ArgumentParser('A long string that goes on and
我是一名优秀的程序员,十分优秀!