Search code examples
pythonexceptiontracebackunit-testing

Python unittest assertraises error


I had this weird trouble running my unittest in Python: I used assertRaises, and running the unittest raised the correct exception, but the test still failed. Ok I cannot really explain it, please see the traceback for yourself:

Error
Traceback (most recent call last):
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/Part1and4Test.py", line 32, in test_non_alpha_name
self.assertRaises(RestNameContainNonAlphaError, RestaurantName(self.non_alpha_name))
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/InputCheck.py", line 29, in __init__
raise RestNameContainNonAlphaError('There are non alphabetic characters that I can not recognize!')
RestNameContainNonAlphaError: There are non alphabetic characters that I can not recognize!


Error
Traceback (most recent call last):
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/Part1and4Test.py", line 24, in test_non_string_name
self.assertRaises(InputNotStringError, RestaurantName, self.non_string_name)
File "/Users/chianti/anaconda/lib/python2.7/unittest/case.py", line 473, in assertRaises
callableObj(*args, **kwargs)
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/InputCheck.py", line 33, in __init__
raise InputNotStringError('Not String! The input is supposed to be a string type!')
InputNotStringError: Not String! The input is supposed to be a string type!

Why ?????????? Any ideas are appreciated !!! THANK YOU


Here is my unittest:

class RestaurantNameTests(unittest.TestCase):

def setUp(self):

    self.non_string_name = 123
    self.valid_name = 'Italian rest '
    self.non_alpha_name = 'valid ** n'

def tearDown(self):

    self.non_string_name = None
    self.valid_name = None
    self.non_alpha_name = None

def test_non_string_name(self):

    with self.assertRaises(InputNotStringError):
        RestaurantName(self.non_string_name)

def test_valid_name(self):

    self.assertEqual(RestaurantName(self.valid_name).__str__(), 'Italian rest')

def test_non_alpha_name(self):

    self.assertRaises(RestNameContainNonAlphaError, RestaurantName(self.non_alpha_name))

If you need to see the definition of RestaurantName, here it is:

class RestaurantName():

def __init__(self, input_contents):

    self.name = input_contents

    if IsValidString(self.name):

        self.no_space_name = self.name.replace(' ', '')

        if str.isalpha(self.no_space_name):
            pass
        else:
            raise RestNameContainNonAlphaError('There are non alphabetic characters that I can not recognize!')

    else:

        raise InputNotStringError('Not String! The input is supposed to be a string type!')

def __repr__(self):

    return 'RestaurantName(%s)' % self.name.strip()  

def __str__(self):

    return self.name.strip() 

Thanks again


Solution

  • The traceback doesn't match your description of the problem (nor your code FWIW). The error you get is in test_non_alpha_name() which you didn't post but from your error message looks like:

    self.assertRaises(
        RestNameContainNonAlphaError, 
        RestaurantName(self.non_alpha_name)
        )
    

    This is not the correct way to use assertRaises(). You must pass ExpectedExceptionClass, callable, *args, **kw to assertRaises, and args and kw will be passed to your callable. IOW you want:

    self.assertRaises(
        RestNameContainNonAlphaError, 
        RestaurantName, 
        self.non_alpha_name
        )
    

    The reason is simple: the way you currently call it, the exception is triggered before the call to assertRaises.

    As a side note:

    • your tearDown method is useless
    • there's already a builtin exception for wrong types, it's named TypeError
    • there's also a builtin exception for wrong values which is named ValueError