我有這個奇怪的麻煩在Python運行我的unittest: 我用assertRaises,並運行unittest引發正確的異常,但測試仍然失敗。好吧,我真的不能解釋它,請看看自己的回溯:Python單元測試聲明錯誤
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!
爲什麼??????????任何想法都很感謝! THANK YOU
這裏是我的單元測試:
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()
再次感謝
請在'Part1and4Test.py'中向我們展示'test_non_alpha_name'功能。 – Kevin 2014-12-04 16:55:15
我很抱歉。對於我的第一個問題描述版本中的錯誤,我表示抱歉。我更新了我的問題。謝謝:) – Chianti5 2014-12-04 17:39:00