【发布时间】:2019-02-10 11:45:06
【问题描述】:
我需要使用 Python 中的单元测试编写测试用例来测试圈子的创建。
-
使用方法
__init__定义一个类Circle,该类使用属性radius初始化一个圆,具有以下限制:-
radius必须是数值,如果不引发类型错误,错误消息“半径必须是数字”。 -
radius两边必须介于 0 到 1000 之间,否则会引发错误消息“半径必须介于 0 到 1000 之间” -
定义一个类方法
area和circumference,它们必须返回四舍五入到小数点后两位的值。
完成类
TestingCircleCreation的定义,该类测试__init__方法的行为,如下所述。 -
-
定义
test_creating_circle_with_numerical_radius的测试方法,创建半径为2.5的圆并检查半径是否与值2.5匹配 -
定义测试方法
test_creating_circle_with_negative_radius,在创建半径为 2.5 的圆时检查是否引发值错误异常并显示错误消息“半径必须介于 0 和 1000 之间”。 -
定义测试方法
test_creating_circle_with_greaterthan_radius检查是否引发ValueError异常并显示错误消息“半径必须介于 0 和 1000 之间”,同时创建半径为 1000.1 的圆。 -
定义测试方法
test_creating_circle_with_nonnumeric_radius,它检查TypeError在创建半径为“hello”的圆时是否引发异常并显示错误消息“radius must be number”。
我在下面尝试过,但失败并出现错误
Traceback (most recent call last):
File "..\Playground\", line 86, in <module>
pass_count = pass_count[0]
IndexError: list index out of range
代码:
import inspect
import re
import unittest
import math
# Define below the class 'Circle' and it's methods with proper doctests.
class Circle:
def __init__(self, radius):
# Define the initialization method below
try:
if not isinstance(radius, (int, float)):
raise TypeError
elif 1000 >=radius>=0:
self.radius=radius
else:
raise ValueError
except ValueError:
raise ValueError("radius must be between 0 and 1000 inclusive")
except TypeError:
raise TypeError("radius must be a number")
def area(self):
# Define the area functionality below
y=math.pi*(self.radius**2)
return round(y,2)
def circumference(self):
# Define the circumference functionality below
x=math.pi*2*self.radius
return round(x,2)
class TestCircleCreation(unittest.TestCase):
def test_creating_circle_with_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# the value of c1.radius equal to 2.5 or not
c1=Circle(2.5)
self.assertEqual(c1.radius,2.5)
def test_creating_circle_with_negative_radius(self):
# Try Defining a circle 'c' with radius -2.5 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
c=Circle(-2.5)
self.assertEqual(c.radius,-2.5)
self.assertRaises(ValueError)
def test_creating_circle_with_greaterthan_radius(self):
# Try Defining a circle 'c' with radius 1000.1 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
c=Circle(1000.1)
self.assertEqual(c.radius,1000.1)
self.assertRaises(ValueError)
def test_creating_circle_with_nonnumeric_radius(self):
# Try Defining a circle 'c' with radius 'hello' and see
# if it raises a TypeError with the message
# "radius must be a number"
c=Circle('hello')
self.assertEqual(c.radius,'hello')
self.assertRaises(TypeError)
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(TestCircleCreation.test_creating_circle_with_numeric_radius)
doc2 = inspect.getsource(TestCircleCreation.test_creating_circle_with_negative_radius)
doc3 = inspect.getsource(TestCircleCreation.test_creating_circle_with_greaterthan_radius)
doc4 = inspect.getsource(TestCircleCreation.test_creating_circle_with_nonnumeric_radius)
assert1_count = len(re.findall(r'assertEqual', doc1))
print(str(assert1_count))
assert1_count = len(re.findall(r'assertEqual', doc2))
assert2_count = len(re.findall(r'assertRaises', doc2))
print(str(assert1_count), str(assert2_count))
assert1_count = len(re.findall(r'assertEqual', doc3))
assert2_count = len(re.findall(r'assertRaises', doc3))
print(str(assert1_count), str(assert2_count))
assert1_count = len(re.findall(r'assertEqual', doc4))
assert2_count = len(re.findall(r'assertRaises', doc4))
print(str(assert1_count), str(assert2_count))
【问题讨论】:
标签: python python-3.x unit-testing