python detect warning

Solutions on MaxInterview for python detect warning by the best coders in the world

showing results for - "python detect warning"
Camilo
24 May 2019
1# credit to Stack Overflow user in source link
2
3import warnings
4
5def fxn():
6    warnings.warn("deprecated", DeprecationWarning)
7
8with warnings.catch_warnings(record=True) as w:
9    # Cause all warnings to always be triggered.
10    warnings.simplefilter("always")
11    # Trigger a warning.
12    fxn()
13    # Verify some things
14    assert len(w) == 1
15    assert issubclass(w[-1].category, DeprecationWarning)
16    assert "deprecated" in str(w[-1].message)