163
社区成员
发帖
与我相关
我的任务
分享
class triangle:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
# 判断是否是三角形
def IsTriangle(self):
if self.a <= 0 or self.b <= 0 or self.c <= 0:
return False
if self.a + self.b <= self.c or self.a + self.c <= self.b or self.b + self.c <= self.a:
return False
return True
# 返回三角形类型:
# 一般三角形、等腰三角形、
# 等边三角形、不构成三角形
def getType(self):
if not self.IsTriangle():
return "不构成三角形"
if self.a == self.b == self.c:
return "等边三角形"
elif self.a == self.b or self.a == self.c or self.b == self.c:
return "等腰三角形"
else:
return "一般三角形"
from triangle import triangle
import unittest
class TestTriangle(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_IsTriangle1(self): # Test case 1
self.tri = triangle(2, 3, 4)
self.IsTriangle = self.tri.IsTriangle()
self.assertEqual(self.IsTriangle, True)
def test_IsTriangle2(self): # Test case 2
tri = triangle(1, 2, 5)
is_triangle = tri.IsTriangle()
self.assertEqual(is_triangle, False)
def test_IsTriangle3(self): # Test case 3
tri = triangle(4, 4, 4)
is_triangle = tri.IsTriangle()
self.assertEqual(is_triangle, True)
if __name__ == '__main__':
# 构造测试集
suite = unittest.TestSuite()
suite.addTest(TestTriangle("test_IsTriangle1"))
suite.addTest(TestTriangle("test_IsTriangle2"))
suite.addTest(TestTriangle("test_IsTriangle3"))
# 执行测试
runner = unittest.TextTestRunner()
runner.run(suite)