Skip to content

OutcomeException should not be a subclass of Exception #580

Closed

Description

@pytestbot

Originally reported by: BitBucket: thiefmaster, GitHub: thiefmaster


Imagine this code:

def blackbox(callback, arg):
    # this is assumed to be application code
    try:
        callback(arg)
    except Exception:
        pass


def test_callback():
    def cb(x):
        if not x:
            import pytest
            pytest.fail('foo')

    blackbox(cb, True)
    blackbox(cb, False)

The testcase will pass since except Exception will catch the Failed raised by pytest.fail. However, I don't think there is any valid use-case for application code to prevent a testcase failure from propagating to the test runner.

A more practical example where this is a major issue is monkeypatching a library function to fail when called. For example, one might want to monkeypatch smtplib.SMTP.connect to fail when code tries to send an email unexpectedly - but chances are good that the application is wrapping that code in a try...except Exception block to prevent an email sending failure from breaking things (e.g. because the email is not very important). So now the pytest.fail called in the monkeypatched method will never propagate since it's caught by application code.


Activity

  1. pytestbot commented on Sep 8, 2014

    @pytestbot
    ContributorAuthor

    Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):


    When following this route, how can one also protect agains try: callback(); except: pass, that is a bare exception in user code?

    (I must admit I've never tried to call pytest.fail() in callbacks before)

  2. pytestbot commented on Sep 8, 2014

    @pytestbot
    ContributorAuthor

    Original comment by BitBucket: thiefmaster, GitHub: thiefmaster:


    That's such an unlikely case though...

  3. added
    type: bug
    on Jun 15, 2015
  4. added this to the 2.8 milestone on Sep 13, 2015
  5. RonnyPfannschmidt commented on Sep 13, 2015

    @RonnyPfannschmidt
    Member

    @flub
    its sensible to use BaseException to help people

    we don't need to protect about the diaper antipattern

  6. modified the milestones: 2.8, on Sep 18, 2015
  7. nicoddemus commented on Sep 25, 2015

    @nicoddemus
    Member

    Shouldn't this be in 2.9.0? It's not a bug per-se, but an improvement that might even break current test suites, IMO.

  8. modified the milestones: 3.0, 2.8.1 on Sep 26, 2015
  9. The-Compiler commented on Aug 5, 2016

    @The-Compiler
    Member

    Changing the milestone for this to 4.0 for now.

  10. modified the milestones: 4.0, 3.0 on Aug 5, 2016
  11. 3 remaining items

  12. added 4 commits that reference this issue on Jun 11, 2017
    c81380d
    d3b0041
    260545e
    693bb32
  13. added a commit that references this issue on Jul 28, 2017
    06a4933
  14. added a commit that references this issue on Jul 30, 2017
    e1aed8c
  15. blueyed commented on May 7, 2019

    @blueyed
    Contributor

    Just for reference/info: using BaseException is problematic, e.g. when people are trying to have a safe repr (and only handling Exception, and where pytest-django might throw a failure for when DB access is not allowed), or with asyncio which only handles Exception (pytest-dev/pytest-asyncio#123).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions