我argparse用来解析命令行参数.
为了帮助调试,我想print在调用Python脚本的参数中添加一行.有没有一种简单的方法可以做到这一点argparse?
我正在开发一个python命令行界面程序,我在测试时发现它很无聊,例如,这里是程序的帮助信息:
usage: pyconv [-h] [-f ENCODING] [-t ENCODING] [-o file_path] file_path
Convert text file from one encoding to another.
positional arguments:
file_path
optional arguments:
-h, --help show this help message and exit
-f ENCODING, --from ENCODING
Encoding of source file
-t ENCODING, --to ENCODING
Encoding you want
-o file_path, --output file_path
Output file path
Run Code Online (Sandbox Code Playgroud)
当我对程序进行更改并想要测试某些内容时,我必须打开一个终端,输入命令(带有选项和参数),输入enter,然后查看运行时是否发生任何错误.如果确实发生了错误,我必须回到编辑器并从头到尾检查代码,猜测bug的位置,进行小的更改,写print行,返回终端,再次运行命令......
递归.
所以我的问题是,使用CLI程序进行测试的最佳方法是什么,它可以像使用普通python脚本进行单元测试一样简单吗?
我正在尝试测试一个接受输入的函数,stdin我目前正在测试这样的东西:
cat /usr/share/dict/words | ./spellchecker.py
Run Code Online (Sandbox Code Playgroud)
在测试自动化的名义,有什么方法pyunit可以伪输入raw_input()?
我正在尝试将 unittest 与使用 argparse 模块并遇到一些困难的程序一起使用。我参考了这篇有用的帖子作为起点,但显然我仍然遗漏了一些东西。
这是基本程序:
#arg_test.py
import sys
import argparse
class Thingy:
def __init__(self, name):
self.name = name
def parse_args(args):
parser = argparse.ArgumentParser(description='description here')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
parser.add_argument('-a', '--arg1', required=True, help='this is for arg1')
parser.add_argument('-b', '--arg2', required=True, help='this is for arg2')
return parser.parse_args()
def main():
parser = Thingy.parse_args(sys.argv[1:])
print('the args are: {}'.format(parser))
if parser.arg1:
print('the value of arg1 is : {}'.format(parser.arg1))
if parser.arg2:
print('the value of arg2 is : {}'.format(parser.arg2))
if __name__ == '__main__':
main() …Run Code Online (Sandbox Code Playgroud) 我正在为argparse实现编写测试用例.我打算测试'-h'功能.以下代码执行此操作.但它也输出脚本的用法.有没有办法压制那个?
self.assertRaises(SystemExit, arg_parse_obj.parse_known_args, ['-h'])
Run Code Online (Sandbox Code Playgroud)
另外,我们可以检查抛出的异常号码吗?例如'-h'抛出SystemExit:0,而无效或不足的args抛出SystemExit:2.有没有办法检查数字代码?
以下粘贴包含来自三个单独的Python文件的相关片段。第一个是从命令行调用的脚本,该脚本在给定某些参数的情况下实例化CIPuller。发生的情况是脚本被调用,类似于:(
script.py ci其他argpar被argparse吞噬)。
第二个是名为的子类的一部分Puller。三是子类的部分Puller叫CIPuller。
当调用正确的子类时,这将非常有用,并且使用错误其他args的任何用户都可以查看给定子类的正确args,以及超类的通用参数。(尽管使我脱机了,也许我应该为此使用argparse子命令。)
我被困试图为这些类编写测试。当前,我需要ArgumentParser实例化这些类,但是在测试中,我不是从命令行实例化事物,因此我ArgumentParser没有用。
我尝试ArgumentParser在测试工具中创建一个,以传递给CIPuller's测试代码中的构造函数,但是如果add_argument在那儿使用argparse,则可以理解,当它add_argument在CIPuller构造函数中调用时,会抱怨双(重复)参数。
有什么合适的设计来用参数测试这些类?
#!/usr/bin/env python
from ci_puller import CIPuller
import argparse
import sys
# Using sys.argv[1] for the argument here, as we don't want to pass that onto
# the subclasses, which should receive a vanilla ArgumentParser
puller_type = sys.argv.pop(1)
parser = argparse.ArgumentParser(
description='Throw data into Elasticsearch.'
)
if puller_type == 'ci': …Run Code Online (Sandbox Code Playgroud) 嗨,我想测试我的可执行模块main.py。在这个模块中有main()一个接受两个参数的函数:
# main.py
def main(population_size: int, number_of_iterations: int):
...
Run Code Online (Sandbox Code Playgroud)
在这个模块的底部有一个接受命令行参数并执行main函数的逻辑:
# main.py
if __name__ == "__main__":
# create parser and handle arguments
PARSER = argparse.ArgumentParser()
PARSER.add_argument("--populationSize",
type=int,
default=-1,
help="Number of individuals in one iteration")
PARSER.add_argument("--numberOfIterations",
type=int,
default=-1,
help="Number of iterations in one run")
# parse the arguments
ARGS = PARSER.parse_args()
main(ARGS.populationSize, ARGS.numberOfIterations)
Run Code Online (Sandbox Code Playgroud)
我想测试传递的命令行参数。我的测试方法不起作用:
# test_main.py
@staticmethod
@mock.patch("argparse.ArgumentParser.parse_args")
@mock.patch("main.main")
def test_passing_arguments(mock_main, mock_argparse):
"""Test passing arguments."""
mock_argparse.return_value = argparse.Namespace(
populationSize=4, numberOfIterations=3)
imp.load_source("__main__", "main.py")
mock_main.assert_called_with(4, 3)
Run Code Online (Sandbox Code Playgroud)
我得到的错误 …
python ×7
argparse ×5
unit-testing ×4
linux ×1
mocking ×1
pytest ×1
python-2.7 ×1
python-3.x ×1
testing ×1