This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yilei_not_used
Recipients yilei_not_used
Date 2020-10-28.20:42:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1603917748.58.0.497118511027.issue42186@roundup.psfhosted.org>
In-reply-to
Content
Because unittest adds a `default` filter before tests run, other warnings filters are overridden if added before.

Ideally, unittest should not make the warnings less serious, e.g. if there is already an 'error' filter that raises exception, it shouldn't "downgrade" to 'default' that simply prints.

The following example, using lib.a_function() raises exception in a regular program, but the unit test passes:

$ cat lib.py
import warnings
class MyWarning(UserWarning):
    pass
warnings.filterwarnings('error', category=MyWarning)
def a_function():
  warnings.warn('Do not use.', MyWarning)

$ cat lib_test.py
import unittest
import lib
class TestLib(unittest.TestCase):
    def test_function(self):
        lib.a_function()
if __name__ == '__main__':
    unittest.main()

$ python -m unittest -v lib_test
test_function (lib_test.TestLib) ... lib.py:6: MyWarning: Do not use.
  warnings.warn('Do not use.', MyWarning)
ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

$ python
>>> import lib
>>> lib.a_function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib.py", line 6, in a_function
    warnings.warn('Do not use.', MyWarning)
lib.MyWarning: Do not use.
History
Date User Action Args
2020-10-28 20:42:28yilei_not_usedsetrecipients: + yilei_not_used
2020-10-28 20:42:28yilei_not_usedsetmessageid: <1603917748.58.0.497118511027.issue42186@roundup.psfhosted.org>
2020-10-28 20:42:28yilei_not_usedlinkissue42186 messages
2020-10-28 20:42:28yilei_not_usedcreate