Catch import warnings in Python -



Catch import warnings in Python -

suppose have file a.py next contents:

import warnings warnings.warn("a deprecated", deprecationwarning)

i want grab warning when import , assert deprecationwarning. i'm doing following:

import warnings warnings.catch_warnings(record=true) w: import

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 stacklevels - 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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -