Skip to content

Instantly share code, notes, and snippets.

@edrabc
Created March 25, 2013 11:47
Show Gist options
  • Save edrabc/5236611 to your computer and use it in GitHub Desktop.
Save edrabc/5236611 to your computer and use it in GitHub Desktop.
pyshould test-case to handle exceptions using Python 2.6
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment