Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest.assertRaises() return the raised exception #53796

Closed
denversc mannequin opened this issue Aug 13, 2010 · 7 comments
Closed

unittest.assertRaises() return the raised exception #53796

denversc mannequin opened this issue Aug 13, 2010 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@denversc
Copy link
Mannequin

denversc mannequin commented Aug 13, 2010

BPO 9587
Nosy @kristjanvalur, @merwok, @voidspace, @vadmium
Files
  • unittest.assertRaises.returnex.v1.patch: Proposed Patch
  • 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-08-13.18:45:01.574>
    created_at = <Date 2010-08-13.14:57:42.403>
    labels = ['type-feature', 'library']
    title = 'unittest.assertRaises() return the raised exception'
    updated_at = <Date 2015-01-13.00:38:20.632>
    user = 'https://bugs.python.org/denversc'

    bugs.python.org fields:

    activity = <Date 2015-01-13.00:38:20.632>
    actor = 'martin.panter'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2010-08-13.18:45:01.574>
    closer = 'michael.foord'
    components = ['Library (Lib)']
    creation = <Date 2010-08-13.14:57:42.403>
    creator = 'denversc'
    dependencies = []
    files = ['18501']
    hgrepos = []
    issue_num = 9587
    keywords = ['patch']
    message_count = 7.0
    messages = ['113781', '113810', '113812', '113813', '113994', '114015', '114041']
    nosy_count = 5.0
    nosy_names = ['kristjan.jonsson', 'eric.araujo', 'michael.foord', 'denversc', 'martin.panter']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue9587'
    versions = ['Python 3.2']

    @denversc
    Copy link
    Mannequin Author

    denversc mannequin commented Aug 13, 2010

    It would be great if unittest.assertRaises() returned the raised exception when it passes. This allows the caller to easily perform further checks on the exception, such as its attribute values. Currently assertRaises() returns None (when it doesn't return a context manager) so changing the return value should not break backwards compatibility.

    I see that this was already discussed in bpo-6275 but I'd like to resurrect the discussion since this is a common scenario in my unit tests, and I assume others. Revisions r76238 and r78110 added the ability to get the exception from the context manager (good) but sometimes using the context manager approach adds unnecessary bloat to already long-winded unit tests.

    I've attached a possible patch for the py3k branch (unittest.assertRaises.returnex.v1.patch). Thank you for (re)considering this topic :) Also, thank you Michael Foord for your recent improvements to unittest... the new features are very much appreciated!

    @denversc denversc mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Aug 13, 2010
    @voidspace
    Copy link
    Contributor

    If you want the exception then use assertRaises in a with statement. The exception is available as an attribute on the context manager.

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented Aug 13, 2010

    That was a bit abrupt, want't it? Denver is well aware of this and said:
    "but sometimes using the context manager approach adds unnecessary bloat to already long-winded unit tests."

    I happen to agree with him and don't see why we can't discuss this some. We never did discuss this fully in bpo-6275, I just tempered my patch a bit to at least get something done.

    I don't think this should be rejected out of hand simply because TBDFL said that its an "'odd' API for a unittest assert method". assertRaises already return a context manager if called without a callable (an odd api?), and it can just as well return an exception if called _with_ a callable.

    @voidspace
    Copy link
    Contributor

    Sorry, it's a reopened bug requesting a feature that has already been considered and rejected previously. Yes I was abrupt, my apologies - I'm trying to clear my backlog before I go away.

    @denversc
    Copy link
    Mannequin Author

    denversc mannequin commented Aug 15, 2010

    Michael: Do you disagree with assertRaises() returning the exception object on principle? Or is this just the consensus that you got from the mailing list, including Guido's comment. My particular use case is that I want to check certain attributes being set on the raised exception and I feel that the context manager approach is overkill for my tests since it's just one method call in the context manager. I don't understand why it is considered "odd" for assertRaises() to return the result for further inspection... I need to get it some way and assertRaises() has a reference to it. Thanks for considering this request further.

    @voidspace
    Copy link
    Contributor

    Providing access to the exception on the context manager was *precisely* to meet the use case of wanting to make assertions about the exception. I tend to agree with Guido that having one of the asserts return something is a bit odd, but irrespective of that I don't think we should have two ways of doing exactly the same thing.

    In general I find that the with statement version of assertRaises looks a lot better than the old way of calling it, so I guess I also disagree that it is "overkill" or adds bloat.

    Sorry guys.

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented Aug 16, 2010

    Pehaps it hasn't been demonstrated before, but just for the sake of argument (and because I'm a persistant bugger), here are the two different cases:

    current:
    ctxt = self.assertRaises(MyException)
    with ctxt:
    foo()
    self.assertEqual(ctxt.exception.value, 1)

    suggested:
    e = self.assertRaises(MyExcetpion, foo)
    self.assertEqual(e.value, 1)

    The inconvenient bit about the current method is having to keep the context manager around. Also note that the current way of looking at the exception object makes it blatantly clear that self.assertRaises() is returning an object. That, imho, breaks the argument about self.assert* methods not returning any info.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant