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

Enable warnings by default in unittest #54744

Closed
ezio-melotti opened this issue Nov 25, 2010 · 13 comments
Closed

Enable warnings by default in unittest #54744

ezio-melotti opened this issue Nov 25, 2010 · 13 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ezio-melotti
Copy link
Member

BPO 10535
Nosy @brettcannon, @ezio-melotti, @merwok, @voidspace, @briancurtin, @sorcio
Files
  • issue10535.diff: working patch (without docs and tests)
  • issue10535-2.diff: patch w/ docs and tests
  • warnings.diff
  • 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/ezio-melotti'
    closed_at = <Date 2013-01-19.22:48:00.526>
    created_at = <Date 2010-11-25.22:08:33.926>
    labels = ['type-feature', 'library']
    title = 'Enable warnings by default in unittest'
    updated_at = <Date 2013-01-19.22:48:00.451>
    user = 'https://github.com/ezio-melotti'

    bugs.python.org fields:

    activity = <Date 2013-01-19.22:48:00.451>
    actor = 'ezio.melotti'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2013-01-19.22:48:00.526>
    closer = 'ezio.melotti'
    components = ['Library (Lib)']
    creation = <Date 2010-11-25.22:08:33.926>
    creator = 'ezio.melotti'
    dependencies = []
    files = ['19821', '19868', '19909']
    hgrepos = []
    issue_num = 10535
    keywords = ['patch']
    message_count = 13.0
    messages = ['122411', '122438', '122470', '122779', '122966', '123104', '131221', '131222', '131251', '180099', '180244', '180254', '180272']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'ezio.melotti', 'eric.araujo', 'michael.foord', 'brian.curtin', 'davide.rizzo']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue10535'
    versions = ['Python 3.2']

    @ezio-melotti
    Copy link
    Member Author

    Warnings should be on by default in unittest so that developers can see them while running the tests even if they are silenced by default in Python.

    The plan is to add a "warnings" argument to TestProgram and the default TextTestRunner:

    • if the argument is passed always use it as a filter (e.g. default, ignore, all, ...) for warnings;
    • if the argument is not passed and sys.warnoptions is not [] (i.e. python has been called with "-W something") don't do anything (i.e. use the warnings specified by -W);
    • if the argument is not passed and sys.warnoptions is [], use 'default' (i.e. show warnings).

    In order to prevent floods of warnings when the deprecated assertEquals, assert_, etc. are used (see bpo-9424), a new type of warning could be created (e.g. _UnittestDeprecationWarning) and filtered so that these warnings are printed only once.

    @ezio-melotti ezio-melotti self-assigned this Nov 25, 2010
    @ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label Nov 25, 2010
    @merwok merwok added the stdlib Python modules in the Lib dir label Nov 26, 2010
    @ezio-melotti
    Copy link
    Member Author

    Here's a working patch (without docs and tests).
    I managed to filter the warnings by message without introducing a new type of warning. As it is, all the warnings related to deprecated unittest methods are printed only once per module, regardless of what methods are used and how many times.
    I'm also thinking that it might be better to include the name of the deprecated method in the message and use three filters for fail* methods, assert* methods, and the assertRegexp methods that will be deprecated. This will show at worst 3 warnings per module, but the assertRegexp and the fail* methods are not so common, so it shouldn't be too nosy.
    One "limitation" of the patch is that the per-module special-casing for unittest is not affect by the warnings argument, but it can be changed when -W is used and no warnings arg is passed.

    Regrtest doesn't seem to use unittest, so it should be fixed separately (probably needs another issue).

    @voidspace
    Copy link
    Contributor

    "I'm also thinking that it might be better to include the name of the deprecated method in the message and use three filters for fail* methods, assert* methods, and the assert*Regexp* methods that will be deprecated."

    That sounds good, well - it sounds *essential*.

    @ezio-melotti
    Copy link
    Member Author

    Attached a new patch.

    A few comments about it:

    1. A new category for UnittestWarnings could still be added to make it easier to filter warnings using the -W python flags and thus providing more control;
    2. I didn't change the deprecation message in the patch -- I will probably do it in a separate commit;
    3. The patch touches a few unrelated unittest/test/test_*.py files that were checking for the number of args explicitely;
    4. In order to test the different combinations of flags/args I created a new _test_warnings.py file called from test_runner.py via subprocess;
    5. unittest warnings are now printed only once even when 'always' is passed as an arg, but not when it's set with -W (this somehow addresses the "limitation" described in my previous message);

    @ezio-melotti
    Copy link
    Member Author

    Committed in r86908. I'll leave this open because there still a few things (proposed in the previous message) that can be changed/improved.

    @briancurtin
    Copy link
    Member

    Here's a patch for the ResourceWarnings that were introduced.

    @voidspace
    Copy link
    Contributor

    Does Brian's patch still need applying? (Can this be closed?)

    @ezio-melotti
    Copy link
    Member Author

    Brian's patch is not yet applied afaik, but I couldn't see the warnings here, so it might be something specific to Windows. Brian, can you confirm if you still see the problem?

    Changing the warning type (to UnittestWarning) and the error message can still be done if you think it's a good idea.

    Since this fix didn't affect regrtest, another issue for it might be necessary too.

    @briancurtin
    Copy link
    Member

    I'm not seeing those warnings anymore, so I think the patch can be ignored.

    @brettcannon
    Copy link
    Member

    Ezio, do you want to create separate issues for any of the TODOs you wanted in http://bugs.python.org/issue10535#msg122779 so we can close this bug?

    @ezio-melotti
    Copy link
    Member Author

    The two TODOs left are:

    1. adding UnittestWarning;
    2. improving the error message for deprecations;

    Technically these are new features, so they can go in 3.4 only, but 2) might be simple enough to be backported.
    Regarding 1) it might be a bit too late, given that most deprecations have been around for 2-3 versions already.

    @brettcannon
    Copy link
    Member

    I wouldn't backport; not worth it.

    Anyway, since I consider this bug closed I'm taking myself off the nosy list.

    @ezio-melotti
    Copy link
    Member Author

    Agreed, closing.

    @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

    5 participants