Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x pytest argparse测试用例不工作_Python 3.x_Pytest_Argparse - Fatal编程技术网

Python 3.x pytest argparse测试用例不工作

Python 3.x pytest argparse测试用例不工作,python-3.x,pytest,argparse,Python 3.x,Pytest,Argparse,我正在使用Python 3.4.6 这是工厂: def create_parser(): """ Create argument parser """ # Input configuration parameters _parser = argparse.ArgumentParser(description='Segments Engine') # Application only parameters _parser.add_argument(

我正在使用Python 3.4.6

这是工厂:

def create_parser():
    """ Create argument parser """

    # Input configuration parameters
    _parser = argparse.ArgumentParser(description='Segments Engine')

    # Application only parameters
    _parser.add_argument(
        '-V', '--version', version='%(prog)s ' + __version__,
        action='version')
    _pasrser.add_argument(
        '-d', '--debug', action='store_true')

    return _parser
测试用例如下所示:

class TestCli(unittest.TestCase):
    """ Test the cli.py module """

    def __init__(self, *args, **kwargs):
        """ Initialize Unit Test """
        super(TestCli, self).__init__(*args, **kwargs)

    def setup(self):
        """ Setup before each test case """
        setup_config(app)
        setup_logging(app)

    def teardown(self):
        """ Tear down after each test case """
        pass

    def test_version(self):
        parser = create_parser()
        with pytest.raises(SystemExit):
            args = vars(parser.parse_args(['', '-V']))

        assert args.version == __version__

    def test_debug(self):
        parser = create_parser()
        args = parser.parse_args(['-d'])

        assert args.debug
pytest tests/test_cli.py
然后我执行如下操作:

class TestCli(unittest.TestCase):
    """ Test the cli.py module """

    def __init__(self, *args, **kwargs):
        """ Initialize Unit Test """
        super(TestCli, self).__init__(*args, **kwargs)

    def setup(self):
        """ Setup before each test case """
        setup_config(app)
        setup_logging(app)

    def teardown(self):
        """ Tear down after each test case """
        pass

    def test_version(self):
        parser = create_parser()
        with pytest.raises(SystemExit):
            args = vars(parser.parse_args(['', '-V']))

        assert args.version == __version__

    def test_debug(self):
        parser = create_parser()
        args = parser.parse_args(['-d'])

        assert args.debug
pytest tests/test_cli.py
我犯了以下错误,我做错了什么

======================================================================================= test session starts =========================================================================================
platform darwin -- Python 3.4.6, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: /Users/francisco.benavides/Documents/src/segments-engine, inifile:
collected 1 item s

tests/test_cli.py F

self = <tests.test_cli.TestCli testMethod=test_version>

    def test_version(self):
        parser = create_parser()
>       args = vars(parser.parse_args(['', '-V']))

tests/test_cli.py:29:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1728: in parse_args
args, argv = self.parse_known_args(args, namespace)
../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1760: in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1966: in _parse_known_args
start_index = consume_optional(start_index)
../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1906: in consume_optional
take_action(action, args, option_string)
../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1834: in take_action
action(self, namespace, argument_values, option_string)
../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1043: in __call__
parser.exit()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = ArgumentParser(prog='pytest', usage=None, description='Segments Engine', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), status = 0, message = None

def exit(self, status=0, message=None):
    if message:
        self._print_message(message, _sys.stderr)
>       _sys.exit(status)
E       SystemExit: 0

../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:2373: SystemExit
---------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------
pytest 0.0.1
================================================================================================================================================测试会话开始=========================================================================================
平台darwin——Python 3.4.6、pytest-3.1.3、py-1.4.34、Plugy-0.4.0
rootdir:/Users/francisco.benavides/Documents/src/segments-engine,文件:
收集1个项目
测试/测试cli.py F
自我=
def测试_版本(自身):
parser=create_parser()
>args=vars(parser.parse_args(['',-V']))
测试/测试脚本副本:29:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../...pyenv/versions/3.4.6/lib/python3.4/argparse.py:1728:in parse_args
args,argv=self.parse\u known\u args(args,名称空间)
../../../...pyenv/versions/3.4.6/lib/python3.4/argparse.py:1760:in parse_known_args
名称空间,args=self.\u解析\u已知\u args(args,名称空间)
../../../../.pyenv/versions/3.4.6/lib/python3.4/argparse.py:1966:in/u parse\u known\u args
开始索引=消耗索引可选(开始索引)
../../../...pyenv/versions/3.4.6/lib/python3.4/argparse.py:1906:in-consume\u可选
执行操作(操作、参数、选项字符串)
../../../...pyenv/versions/3.4.6/lib/python3.4/argparse.py:1834:in-take\u-action
操作(自身、名称空间、参数值、选项字符串)
../../../...pyenv/versions/3.4.6/lib/python3.4/argparse.py:1043:in\uu调用__
parser.exit()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self=ArgumentParser(prog='pytest',usage=None,description='Segments Engine',formatter\u class=,conflict\u handler='error',add\u help=True),status=0,message=None
def退出(自我,状态=0,消息=无):
如果消息:
self.\u print\u message(message,\u sys.stderr)
>_系统退出(状态)
E系统出口:0
../../../...pyenv/versions/3.4.6/lib/python3.4/argparse.py:2373:SystemExit
----------------------------------------------------------------------------------------捕获的stdout调用----------------------------------------------------------------------------------------
pytest 0.0.1

我尝试了几种谷歌搜索的方法,但到目前为止没有任何帮助。

只是一个猜测,因为您还没有向我们展示您的
创建解析器
实现:

-V
是一个“版本”类型的命令(?),如果您读到版本为

“[打印]版本信息并在调用时[退出]”

您需要捕获退出的
,这里有一种方法:

with pytest.raises(SystemExit):
    parser.parse_args(['', '-V'])

# maybe use `capsys` to verify that the version was printed
out, err = capsys.readouterr()
# ...