try:
    foo = bar
except Exception as exception:
    assert repr(exception) == 'NameError("name \'bar\' is not defined",)'
name = repr(exception).split('(')[0]
assert name == 'NameError'
How to get name of exception that was caught in Python?