homepage

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.

classification
Title: unittest overrides more serious warnings filter added before unittest.main()
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, terry.reedy, xtreak, yilei_not_used
Priority: normal Keywords:

Created on 2020年10月28日 20:42 by yilei_not_used, last changed 2022年04月11日 14:59 by admin.

Messages (4)
msg379843 - (view) Author: Not Used (yilei_not_used) Date: 2020年10月28日 20:42
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.
msg379850 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020年10月29日 00:34
Seems to be a duplicate of https://bugs.python.org/issue15626. See also https://bugs.python.org/issue31975 
msg380016 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020年10月30日 23:59
Nick and/or Victor: should this be closed in favor of #15626? or should the latter be closed as mostly fixed, with this being left open as a followup?
msg380039 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2020年10月31日 05:15
Closing the old one as partially fixed, and linking here as a superseder makes sense to me, so I went ahead and did that.
History
Date User Action Args
2022年04月11日 14:59:37adminsetgithub: 86352
2020年10月31日 21:14:17vstinnersetnosy: - vstinner
2020年10月31日 05:15:34ncoghlansetmessages: + msg380039
2020年10月31日 05:07:43ncoghlanlinkissue15626 superseder
2020年10月30日 23:59:51terry.reedysetnosy: + vstinner, terry.reedy

messages: + msg380016
versions: - Python 3.6, Python 3.7
2020年10月29日 00:34:46xtreaksetnosy: + ncoghlan
messages: + msg379850
2020年10月29日 00:25:05xtreaksetnosy: + xtreak
2020年10月28日 20:42:28yilei_not_usedcreate

AltStyle によって変換されたページ (->オリジナル) /