Message379843
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. |
|
Date |
User |
Action |
Args |
2020-10-28 20:42:28 | yilei_not_used | set | recipients:
+ yilei_not_used |
2020-10-28 20:42:28 | yilei_not_used | set | messageid: <1603917748.58.0.497118511027.issue42186@roundup.psfhosted.org> |
2020-10-28 20:42:28 | yilei_not_used | link | issue42186 messages |
2020-10-28 20:42:28 | yilei_not_used | create | |
|