最小的测试套件在两个测试中失败,即使出现了一个值错误,而且我已经打印了一个字以确保条件被激活了,而且确实被激活了,有人知道这两个测试失败了吗?
import string
class PhoneNumber:
def __init__(self, number):
self.number = ''.join([el for el in number if el in string.digits])
if len(self.number) <= 9 or len(self.number) > 11:
raise ValueError('Number length is incorrect')
elif len(self.number) == 11:
if self.number.startswith('1'):
self.number = self.number[1::]
if self.number.startswith('0') or self.number.startswith('1'):
print('+++ This fires +++')
raise ValueError('Incorrect area code')
else:
raise ValueError('Number does not start with 1')
elif self.number[0] == '0' or self.number[0] == '1':
raise ValueError('Incorrect area code')
elif self.number[3] == '0' or self.number[3] == '1':
raise ValueError('Incorrect exchange code')
def area_code(self):
return self.number[:3]
def pretty(self):
return '('+ self.number[:3] +') ' + self.number[3:6] + '-' + self.number[6::]
PhoneNumber("1 (023) 456-7890")
def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number(self):
with self.assertRaisesWithMessage(ValueError):
PhoneNumber("1 (023) 456-7890")
def test_invalid_if_area_code_starts_with_1_on_valid_11_digit_number(self):
with self.assertRaisesWithMessage(ValueError):
PhoneNumber("1 (123) 456-7890")
+++ This fires +++
Traceback (most recent call last):
File "Exercism/python/phone-number/phone_number.py", line 28, in <module>
PhoneNumber("1 (023) 456-7890")
File "Exercism/python/phone-number/phone_number.py", line 14, in __init__
raise ValueError('Incorrect area code')
ValueError: Incorrect area code
=========================================================================== FAILURES ===========================================================================
_____________________________________ PhoneNumberTest.test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number _____________________________________
self = <phone_number_test.PhoneNumberTest testMethod=test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number>
def test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number(self):
with self.assertRaisesWithMessage(ValueError):
> PhoneNumber("1 (223) 056-7890")
E AssertionError: ValueError not raised
phone_number_test.py:75: AssertionError
_____________________________________ PhoneNumberTest.test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number _____________________________________
self = <phone_number_test.PhoneNumberTest testMethod=test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number>
def test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number(self):
with self.assertRaisesWithMessage(ValueError):
> PhoneNumber("1 (223) 156-7890")
E AssertionError: ValueError not raised
phone_number_test.py:79: AssertionError
=================================================================== short test summary info ====================================================================
FAILED phone_number_test.py::PhoneNumberTest::test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number - AssertionError: ValueError not raised
FAILED phone_number_test.py::PhoneNumberTest::test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number - AssertionError: ValueError not raised
================================================================= 2 failed, 19 passed in 0.78s =================================================================
您使用的是AssertraisesWithMessage
,这似乎不是UnitTest.TestCase
中的默认方法,但我认为它需要一个消息,默认为空消息。 如果不提供消息,则检查失败。
如果要使用标准的UnitTest
方法,可以使用:
def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number(self):
with self.assertRaises(ValueError):
PhoneNumber("1 (023) 456-7890")
或者,如果您想测试消息:
def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number(self):
with self.assertRaisesRegex(ValueError, "Incorrect area code"):
PhoneNumber("1 (023) 456-7890")
话虽如此,但看起来您正在混合unittest
和pytest
。 虽然这是可能的,但我建议不要这样做,除了旧的unittest
代码。
使用pytest
,测试如下所示:
def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number(self):
with pytest.raises(ValueError):
PhoneNumber("1 (023) 456-7890")
或
def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number(self):
with pytest.raises(ValueError, match="Incorrect area code"):
PhoneNumber("1 (023) 456-7890")
您需要删除phone_number.py
中的最后一行,在该行中实例化了一个无效的phoneNumber
。 这会导致在测试运行之前就发生错误。 我猜你把那行字留下是因为打错了吧?
为了清楚起见,下面这行是:
PhoneNumber("1 (023) 456-7890")
您正在测试这一行是否无效,但它始终在phone_number.py
模块中运行。