Catch import warnings in Python -
Catch import warnings in Python -
suppose have file a.py
next contents:
i want grab warning when import , assert deprecationwarning
. i'm doing following:
and trying assert using assert_equal(w[0].category, deprecationwarning)
shows w
empty. isn't catching warning guess. there other way this?
edit: forgot add together tried warnings.simplefilter("always")
, no warning recorded.
edit 2: have warning levels. [see comments]
edit 3: tried different stacklevel
s - 0, 1, 2, 3. no effect :|
you need add together line
warnings.simplefilter("always")
to code, this:
with warnings.catch_warnings(record=true) w: warnings.simplefilter("always") import
so, working version of 'b.py' (as were) might this:
import warnings import unittest class testwarnings(unittest.testcase): def test_warnings(self): warnings.catch_warnings(record=true) w: warnings.simplefilter("always") import self.assertequal(w[0].category, deprecationwarning) if __name__ == '__main__': unittest.main()
python
Comments
Post a Comment