pytest argparse test case does not work

Viewed 1477

I'm using Python 3.4.6.

Here is the factory:

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

The test case is as follows:

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

Then I execute as follows:

pytest tests/test_cli.py

I get the following errors, what am I doing wrong?

======================================================================================= 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

I have tried several ways I found googling, but so far nothing has helped me.

1 Answers
Related