New submission from Yilei Yang <[email protected]>:
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.
----------
components: Library (Lib)
messages: 379843
nosy: yilei
priority: normal
severity: normal
status: open
title: unittest overrides more serious warnings filter added before
unittest.main()
type: behavior
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42186>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com