argparse sys.exit finishes test execution

Issue #132 wontfix
Günter Walser
created an issue

When executing a script which uses argparse and testing for --help then py.test exits with E SystemExit: 0 , even if the sys.exit is the desired behaviour.

I couldn't find any hint in the documentation if this can be somehow disabled. Maybe somebody has a hint / an idea.

Here some code to replicate it.

Thanks

Günter

====== proj\an_app.py ======

{{{

!python

!/usr/bin/env python

import argparse import os

class Application(object):

def __init__(self, argv):
    parser = argparse.ArgumentParser(description='Test what happens when argparse exits.',
                                     prog="[python] %s" % (os.path.basename(argv[0])))
    parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.0.2')
    parser.add_argument('--path', metavar='P', default='a_path',
                        help='A path.')
    args = parser.parse_args(argv[1:])
    # exit(2)  TODO exit and py.test

if name == 'main': import sys app = Application(sys.argv) }}}

====== proj\test\basic_test.py: ====== {{{

!python

import sys sys.path.append('D:\opt\deve\workspace\proj')

from an_app import Application

def test_help(capsys): app = Application(['py.test', '--help']) assert True }}}

executing py.test results in a long trace and finally

D:\pyt\27\lib\argparse.py:1758:


self = _HelpAction(option_strings=['-h', '--help'] , dest='help', nargs=0, const=None,... type=None, choices=None, help='show this help message and exi t', metavar=None) parser = ArgumentParser(prog='[python] py.test', u sage=None, description='Test what hap...lass=<clas s 'argparse.HelpFormatter'>, conflict_handler='err or', add_help=True) namespace = Namespace(path='a_path'), values = [] option_string = '--help'

def __call__(self, parser, namespace, values,

option_string=None): parser.print_help()

  parser.exit()

{{{

!shell

D:\pyt\27\lib\argparse.py:980:


self = ArgumentParser(prog='[python] py.test', usa ge=None, description='Test what hap...lass=<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.stde

rr)

  _sys.exit(status)

E SystemExit: 0

D:\pyt\27\lib\argparse.py:2299: SystemExit =========== 1 failed in 0.58 seconds ============

}}}

Comments (4)

  1. Günter Walser reporter

    Yes, it makes sense and works.

    Maybe a hint for people like me with limited knowledge and not being aware of the fact, that an exception SystemExit is part of basic Python could help there. Because I did spent considerable effort to look for sys.exit() and py.test Google matches.

    This could be either a

    Note: Some python packages use sys.exit() to exit. Be aware that sys.exit() raises exception SystemExit and thus must be handled with the raises helper.

    if you have something like this in your docu concept or maybe an extension of the example to have a second function using sys.exit().

    Thanks for the tool and the advice,

    Günter

  2. Holger Krekel repo owner

    I wonder where the best place for placing such a note would be. I guess there should be an example section about testing command line scripts which is the primary case where testing of code happens which raises SystemExit. I guess when i put the new docs online at http://pytest.org i hope/think your mentioned google searched would then turn it up.

  3. Log in to comment