Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing (unittest) if a variable is a nonempty string in python the right way?

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!

like image 566
Aufwind Avatar asked Nov 29 '25 22:11

Aufwind


1 Answers

Is this acceptable or am I making heavy rookie mistake?

Yes and No.

It's a fine example of how to write a unit test.

But it's a use case that shouldn't exist in your code in the first place.

class FooBar(object):
    def take_a_string_and_do_something(self, param):
        # process nonempty string
        # If they couldn't provide a non-empty string, they get an exception.

You can still test it like this.

class TestFooBar(unittest.TestCase):
    def setUp( self ):
        self.foo= FooBar()
    def test_zero_length_should_fail(self):
        self.failUnlessRaises(IndexError, foo.take_a_string_and_do_something, '')
    def test_none_should_fail(self):
        self.failUnlessRaises(TypeError, foo.take_a_string_and_do_something, None)
    def test_non_string_should_fail(self):
        self.failUnlessRaises(TypeError, foo.take_a_string_and_do_something, 234)

Notice that it's much simpler and also much more reliable, since you're not attempting to duplicate Python's extensive internal error-checking.

like image 103
S.Lott Avatar answered Dec 02 '25 12:12

S.Lott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!