def fxn():
    warnings.warn('deprecated', DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
    warnings.simplefilter('always')
fxn()
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
assert 'deprecated' in str(w[-1].message)
In Python, how does one catch warnings as if they were exceptions?