unittest for none type in python?

peppy picture peppy · Feb 14, 2013 · Viewed 25.3k times · Source

I was just wondering how I would go about testing for a function that does not return anything. for example, say I have this function:

def is_in(char):
    my_list = []
    my_list.append(char)

and then if I were to test it:

class TestIsIn(unittest.TestCase):

    def test_one(self):
    ''' Test if one character was added to the list'''
    self.assertEqual(self.is_in('a'), and this is where I am lost)

I don't know what to assert the function is equal to, since there is no return value that I could compare it to.

EDIT: would assertIn work?

Answer

Kevin Christopher Henry picture Kevin Christopher Henry · Feb 12, 2014

All Python functions return something. If you don't specify a return value, None is returned. So if your goal really is to make sure that something doesn't return a value, you can just say

self.assertIsNone(self.is_in('a'))

(However, this can't distinguish between a function without an explicit return value and one which does return None.)