import unittest from pyshould import should class ExceptionTestCase(unittest.TestCase): """ Simple tests for exception assertions """ def test_expect_throw_non_empty_constructor_exception(self): with should.throw(NonEmptyConstructorException): raise NonEmptyConstructorException([]) def test_expect_throw_str_exceptions(self): with should.throw(KeyError): raise KeyError() with should.throw(KeyError): object = {} item = object['non-existing-key'] class NonEmptyConstructorException(Exception): parameter = [] @property def text(self): return self.parameter def __init__(self, parameter, message=None): if parameter is None: raise TypeError("parameter could not be None") self.parameter = parameter self.message = message def __str__(self): return unicode(self).encode('utf-8') def __unicode__(self): return 'Missing fields: %s' % (self.parameter) if __name__ in ('main', '__main__'): unittest.main()