python / cpython

The Python programming language
https://www.python.org
Other
61.86k stars 29.75k forks source link

assertWarns and assertWarnsRegexp #53963

Closed pitrou closed 13 years ago

pitrou commented 13 years ago
BPO 9754
Nosy @brettcannon, @pitrou, @giampaolo, @ezio-melotti, @voidspace, @florentx
Files
  • assertwarns.patch
  • assertwarns2.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: ```python assignee = None closed_at = created_at = labels = ['type-feature', 'library'] title = 'assertWarns and assertWarnsRegexp' updated_at = user = 'https://github.com/pitrou' ``` bugs.python.org fields: ```python activity = actor = 'ezio.melotti' assignee = 'none' closed = True closed_date = closer = 'pitrou' components = ['Library (Lib)'] creation = creator = 'pitrou' dependencies = [] files = ['18728', '18756'] hgrepos = [] issue_num = 9754 keywords = ['patch'] message_count = 14.0 messages = ['115434', '115436', '115449', '115450', '115451', '115453', '115455', '115460', '115465', '115469', '115470', '115471', '115614', '115730'] nosy_count = 6.0 nosy_names = ['brett.cannon', 'pitrou', 'giampaolo.rodola', 'ezio.melotti', 'michael.foord', 'flox'] pr_nums = [] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue9754' versions = ['Python 3.2'] ```

    pitrou commented 13 years ago

    Similar to assertRaises and assertRaisesRegexp, unittest should provide assertWarns and assertWarnsRegexp, to check that a given callable (or piece of code) triggers a particular warning.

    Currently, you have to do that manually using a mixture of warnings.catch_warnings and warnings.filterwarnings, which is pretty annoying.

    voidspace commented 13 years ago

    +1

    pitrou commented 13 years ago

    The __warningregistry__ stuff looks horrible.

    eda57068-96ad-4b33-8431-9c528f59a6a6 commented 13 years ago

    unittest should provide assertWarns and assertWarnsRegexp

    +1 (the internal helpers in test.support could be refactored)

    The __warningregistry__ stuff looks horrible.

    +1 (and it does not behave exactly the same in all Python versions)

    giampaolo commented 13 years ago

    +1 from me as well.

    pitrou commented 13 years ago

    Here is a patch. The approach is different from support.check_warnings(), and tries to mimic assertRaises* instead.

    pitrou commented 13 years ago

    There was some dead code in the patch.

    voidspace commented 13 years ago

    (Note that in general I am against extending the TestCase API with more asserts given how wide it is and how much it has expanded in recent versions. I've written warning checking code enough times for third party projects that I think this is worth it though.)

    eda57068-96ad-4b33-8431-9c528f59a6a6 commented 13 years ago

    Patch looks good. However tests do not pass with -Werror (while test_warnings and others pass).

    Is there a way to catch multiple warnings on a single logical line? (With assertRaises we don't have such use case)

    pitrou commented 13 years ago

    Patch looks good. However tests do not pass with -Werror (while test_warnings and others pass).

    Is there a way to catch multiple warnings on a single logical line?

    I thought we could beef up the API with additional arguments. That would be at the expense of removing the "callable" argument (and mandating the use as a context manager), though, otherwise the signature gets too complicated.

    voidspace commented 13 years ago

    Why not accepting a tuple of warnings? That doesn't make sense for assertWarnsRegexp of course.

    pitrou commented 13 years ago

    Why not accepting a tuple of warnings?

    It already does (and there's a test!).

    That doesn't make sense for assertWarnsRegexp of course.

    True.

    pitrou commented 13 years ago

    Updated patch so that the tests pass with -Werror. Do you think this should be committed before the next alpha?

    pitrou commented 13 years ago

    Committed in r84563!