Can Python assert
be used to check for specific exceptions in a function? For instance, if I have a function that I know will raise a KeyError
, can assert
detect it? For example:
def get_value(x):
lookup = {'one': 1}
return lookup[x]
assert get_value('two') == KeyError
When I run this I just get the KeyError
exception. Can assert
check something like this? Or is that not what assert
is used for?