Skip to content

sys.exit() in a test causes a test run to die #54820

Closed

Description

@voidspace
BPO 10611
Nosy @rbtcollins, @merwok, @voidspace, @serhiy-storchaka
Files
  • issue_10611.diff: patch against py3k
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/voidspace'
    closed_at = <Date 2010-12-19.14:56:28.056>
    created_at = <Date 2010-12-03.01:25:17.075>
    labels = ['easy', 'type-bug']
    title = 'sys.exit() in a test causes a test run to die'
    updated_at = <Date 2021-09-17.13:18:30.937>
    user = 'https://github.com/voidspace'

    bugs.python.org fields:

    activity = <Date 2021-09-17.13:18:30.937>
    actor = 'serhiy.storchaka'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2010-12-19.14:56:28.056>
    closer = 'michael.foord'
    components = []
    creation = <Date 2010-12-03.01:25:17.075>
    creator = 'michael.foord'
    dependencies = []
    files = ['19994']
    hgrepos = []
    issue_num = 10611
    keywords = ['patch', 'easy']
    message_count = 9.0
    messages = ['123153', '123717', '123718', '123726', '124110', '124356', '124357', '230788', '230790']
    nosy_count = 5.0
    nosy_names = ['rbcollins', 'eric.araujo', 'michael.foord', 'ysj.ray', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10611'
    versions = ['Python 2.7', 'Python 3.2']

    Activity

    1. voidspace commented on Dec 3, 2010

      @voidspace
      ContributorAuthor

      Reported by a unittest2 user.

      A SystemExit (or GeneratorExit) will cause a test run to stop in 2.7 / 3.2. This would just be reported as an error in 2.6.

      >>> from unittest import TestCase
      >>> def test(s):
      ...  raise GeneratorExit
      ... 
      >>> class T(TestCase):
      ...  test = test
      ... 
      >>> t = T('test')
      >>> t.run()

      Above code works in Python 2.6 (the exception is caught by TestCase.run) but dies in 2.7 / 3.2

    2. self-assigned this
      on Dec 3, 2010
    3. added
      type-bug
      on Dec 3, 2010
    4. changed the title [-]sys.exit() in a test causes the run to stp[/-] [+]sys.exit() in a test causes a test run to die[/+] on Dec 3, 2010
    5. ysjray commented on Dec 10, 2010

      ysjraymannequin
      Mannequin

      Agreed. I think the "except Exception" in TestCase.run() should be "except BaseException", since BaseException could catch Exception, SystemExit, GeneratorExit, KeyboardInterrupt. The KeyboardInterrupt should be caught first. The remaining three is exactly what is needed.

      Here is a patch I worked, with unittest.

    6. merwok commented on Dec 10, 2010

      @merwok
      Member

      LGTM.

    7. voidspace commented on Dec 10, 2010

      @voidspace
      ContributorAuthor

      At the moment exception handling for setUp / tearDown / testMethod and cleanUp functions are all handled separately. They all have to call addError and as a result we have inconsistent handling of skips, expected failures (etc). There are separate issues for handling expected failures in setUp and skips in tearDown.

      I'd like to fix all these issues by moving the exception handling into a single method and unifying the reporting of failure / error / expected failure / skip test. This will fix all these issues and nicely simplify the implementation.

    8. ysjray commented on Dec 16, 2010

      ysjraymannequin
      Mannequin

      I'd like to fix all these issues by moving the exception handling into a single method and unifying the reporting of failure / error / expected failure / skip test. This will fix all these issues and nicely simplify the implementation.

      That sounds good.

    9. voidspace commented on Dec 19, 2010

      @voidspace
      ContributorAuthor

      Committed to Python 2.7 in revision 87406.

    10. voidspace commented on Dec 19, 2010

      @voidspace
      ContributorAuthor

      Committed to py3k in revision 87390.

    11. rbtcollins commented on Nov 7, 2014

      @rbtcollins
      Member

      Hmm, so testtools went in a different direction here - the same unification stuff, but see testing-cabal/testtools@18bc574

      tl;dr - I think unittest should not prevent this causing the process to exit (but it should still fail the test and fail the test run as a whole), analgous to how KeyboardInterrupt is handled.

    12. voidspace commented on Nov 7, 2014

      @voidspace
      ContributorAuthor

      Allowing sys.exit() to end the test run was particularly a problem for testing command line tools, where improper patching / unexpected code paths would trigger a sys.exit.

      If a test framework author wants a way to end the test run I'm happy to provide that (a custom exception to raise or a flag to turn off sys.exit handling), but I don't think having sys.exit kill test runs is best for test authors.

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    easytype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions