파이썬에서 unittest를 실행하는 데 이상한 문제가 발생했습니다. assertRaises를 사용하고 unittest를 실행하면 올바른 예외가 발생하지만 테스트는 여전히 실패합니다. 좋습니다. 직접 설명 할 수는 없습니다. 직접 문의하십시오.Python unittest assertraises error
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()
다시 한번 감사
'Part1and4Test.py'에'test_non_alpha_name' 함수를 보여주십시오. – Kevin
죄송합니다. 문제 설명의 첫 번째 버전에서 실수 한 것에 대해 사과드립니다. 나는 내 질문을 업데이트했다. 고마워요. – Chianti5