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)