Python unittest断言错误

时间:2014-12-04 16:52:54

标签: python exception traceback unit-testing

我在Python中运行unittest时遇到了这个奇怪的问题:   我使用了assertRaises,并且运行unittest引发了正确的异常,但测试仍然失败了。好吧我无法解释它,请亲自看看追溯:

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!

为什么??????????任何想法都赞赏!!!谢谢你


这是我的单位测试:

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))

如果您需要查看RestaurantName的定义,请输入:

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() 

再次感谢

1 个答案:

答案 0 :(得分:2)

回溯不符合您对问题的描述(也不是您的代码FWIW)。您收到的错误是test_non_alpha_name(),但您没有发布,但是您的错误信息如下:

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

这不是使用assertRaises()的正确方法。您必须将ExpectedExceptionClass, callable, *args, **kw传递给assertRaisesargskw将传递给您的可调用对象。你想要的:

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

原因很简单:您当前调用它的方式,在调用assertRaises之前触发异常。

作为旁注:

  • 你的tearDown方法没用了
  • 已经有错误类型的内置异常,它名为TypeError
  • 还有错误值的内置异常,名为ValueError