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

assertWarns and assertWarnsRegexp #53963

Closed
pitrou opened this issue Sep 3, 2010 · 14 comments
Closed

assertWarns and assertWarnsRegexp #53963

pitrou opened this issue Sep 3, 2010 · 14 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented Sep 3, 2010

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:

    assignee = None
    closed_at = <Date 2010-09-06.19:26:14.323>
    created_at = <Date 2010-09-03.10:50:15.891>
    labels = ['type-feature', 'library']
    title = 'assertWarns and assertWarnsRegexp'
    updated_at = <Date 2010-11-01.15:00:13.320>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2010-11-01.15:00:13.320>
    actor = 'ezio.melotti'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-09-06.19:26:14.323>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2010-09-03.10:50:15.891>
    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
    Copy link
    Member Author

    pitrou commented Sep 3, 2010

    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.

    @pitrou pitrou added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 3, 2010
    @voidspace
    Copy link
    Contributor

    +1

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 3, 2010

    The __warningregistry__ stuff looks horrible.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Sep 3, 2010

    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
    Copy link
    Contributor

    +1 from me as well.

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 3, 2010

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

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 3, 2010

    There was some dead code in the patch.

    @voidspace
    Copy link
    Contributor

    (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.)

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Sep 3, 2010

    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
    Copy link
    Member Author

    pitrou commented Sep 3, 2010

    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
    Copy link
    Contributor

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

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 3, 2010

    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
    Copy link
    Member Author

    pitrou commented Sep 4, 2010

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

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 6, 2010

    Committed in r84563!

    @pitrou pitrou closed this as completed Sep 6, 2010
    @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

    3 participants