[英]Error while testing the TestAreaCreation in Python using Unittest
題:
使用方法 init 定義一個類 Circle,該方法初始化一個具有屬性半徑的圓,具有以下限制。
- 半徑必須是數值,否則會引發類型錯誤並顯示錯誤消息“半徑必須是數字”。
- 半徑必須介於 0 到 1000 之間(包括兩端),如果不引發值錯誤並顯示錯誤消息“半徑必須介於 0 和 1000 之間”
- 定義一個類方法區域和周長,它必須返回四舍五入到 2 位小數的值。
完成類 TestCircleArea 的定義,該類測試以下規范的 area 方法的行為。
定義測試方法 test_circlearea_with_random_numeric_radius ,它創建半徑為 2.5 的圓 c1 並檢查其計算面積是否與值 19.63 匹配
定義測試方法 test_circlearea_with_min_radius ,它創建半徑為 0 的圓 c2 並檢查其計算面積是否與值 0 匹配
定義測試方法 test_circlearea_with_max_radius ,它創建半徑為 1000 的圓 c3 並檢查其計算面積是否與值 3141592.65 匹配
嘗試的解決方案:
import inspect
import re
import unittest
import math
class Circle:
def __init__(self, radius):
# Define the initialization method below
self.radius=radius
if not isinstance(self.radius,(int,float)):
raise TypeError("radius must be a number")
elif(self.radius>1000 or self.radius<0):
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
pass
def area(self):
# Define the area functionality below
return math.pi*(self.radius**2)
def circumference(self):
return 2*math.pi*self.radius
# Define the circumference functionality below
class TestCircleArea(unittest.TestCase):
def test_circlearea_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5, and check if
# its area is 19.63.
c1=Circle(2.5)
self.assertEqual(c1.area(), 2.5)
def test_circlearea_with_min_radius(self):
# Define a circle 'c2' with radius 0, and check if
# its area is 0.
c2=Circle(0)
self.assertEqual(c2.area(), 0)
def test_circlearea_with_max_radius(self):
# Define a circle 'c3' with radius 1000.1. and check if
# its area is 3141592.65.
c3=Circle(1000)
self.assertEqual(c3.area(), 3141592.65)
if __name__ == '__main__':
fptr = open('output.txt', 'w')
runner = unittest.TextTestRunner(fptr)
unittest.main(testRunner=runner, exit=False)
fptr.close()
with open('output.txt') as fp:
output_lines = fp.readlines()
pass_count = [ len(re.findall(r'\.', line)) for line in output_lines if line.startswith('.')
and line.endswith('.\n')]
pass_count = pass_count[0]
print(str(pass_count))
doc1 = inspect.getsource(TestCircleArea.test_circlearea_with_random_numeric_radius)
doc2 = inspect.getsource(TestCircleArea.test_circlearea_with_min_radius)
doc3 = inspect.getsource(TestCircleArea.test_circlearea_with_max_radius)
assert1_count = len(re.findall(r'assertEqual', doc1))
print(str(assert1_count))
assert1_count = len(re.findall(r'assertEqual', doc2))
print(str(assert1_count))
assert1_count = len(re.findall(r'assertEqual', doc3))
print(str(assert1_count))
錯誤:
Traceback (most recent call last):
File "Solution.py", line 61, in <module>
pass_count = pass_count[0]
IndexError: list index out of range
請幫助有什么問題。
我剛剛修改了init方法,它在黑客級別對我有用。
班級圈:
def __init__(self, radius):
# Define the initialization method below
pattern=re.compile("^\\-?[0-9]")
if(pattern.math(str(radius))):
if(radius>=0 and radius<=1000):
self.radius=radius
else:
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
raise TypeError("radius must be a number")
def area(self):
# Define the area functionality below
return math.pi*(self.radius**2)
def circumference(self):
return 2*math.pi*self.radius
# Define the circumference functionality below
類 TestCircleArea(unittest.TestCase):
def test_circlearea_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5, and check if
# its area is 19.63.
c1=Circle(2.5)
return c1.area
return self.assertEqual(c1.area,19.3)
def test_circlearea_with_min_radius(self):
# Define a circle 'c2' with radius 0, and check if
# its area is 0.
c2=Circle(0)
return c2.area
return self.assertEqual(c2.area,0)
def test_circlearea_with_max_radius(self):
# Define a circle 'c3' with radius 1000.1. and check if
# its area is 3141592.65.
c3=Circle(1000)
return c3.area
return self.assertEqual(c3.area,3141592.65)
修改驅動代碼:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.