Maybe this question is trivial, but I am still trying to warm up with unittests in python, so please have patience with me. :-) While trying to write some tests of my own, the following question araised. Assuming a function that processes nonempty strings:
class BadInputError(Exception): pass
class FooBar(object):
def take_a_string_and_do_something(param):
if param == '':
raise BadInputError('param should not be an empty string')
if param is None:
raise BadInputError('param should not be None')
if not isinstance(param, basestring):
raise BadInputError('param must be of type string)
# process nonempty string
The first thing I wanted to make sure (by unittests) is that param is only a nonempty string. So I wrote my testcases this way.
class TestFooBar(unittest.TestCase):
def test_take_a_string_and_do_something(self):
foo = FooBar()
self.failUnlessRaises(BadInputError, foo.take_a_string_and_do_something, '')
self.failUnlessRaises(BadInputError, foo.take_a_string_and_do_something, None)
self.failUnlessRaises(BadInputError, foo.take_a_string_and_do_something, 234)
Is this acceptable or am I making heavy rookie mistake? Your feedback means a lot!
ValueError