163
社区成员
发帖
与我相关
我的任务
分享
class Triangle:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
# 判断是否是三角形
def IsTriangle(self):
if self.a + self.b > self.c and self.b + self.c + self.a and self.a + self.c > self.b:
return True;
return False;
def getType(self):
if self.a == self.b == self.c and self.IsTriangle():
return "等边三角形";
elif (self.a == self.b or self.b == self.c or self.a == self.c) and self.IsTriangle():
return "等腰三角形";
elif self.IsTriangle():
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): # 一般三角形
self.tri =Triangle(3,4,5)
self.IsTriangle = self.tri.IsTriangle()
self.assertEqual(self.IsTriangle, True)
def test_IsTriangle2(self): # 无法构成三角形
self.tri = Triangle(3,3,6)
self.IsTriangle = self.tri.IsTriangle()
self.assertEqual(self.IsTriangle, False)
def test_IsTriangle3(self): # 等边三角形
self.tri = Triangle(6,6,6)
self.assertEqual(self.tri.getType(), "等边三角形")
def test_IsTriangle4(self): # 等腰三角形
self.tri = Triangle(3,3,2)
self.assertEqual(self.tri.getType(), "等腰三角形")
if __name__ == '__main__':
# 构造测试集
suite = unittest.TestSuite()
suite.addTest(TestTriangle("test_IsTriangle1"))
suite.addTest(TestTriangle("test_IsTriangle2"))
suite.addTest(TestTriangle("test_IsTriangle3"))
suite.addTest(TestTriangle("test_IsTriangle4"))
# 执行测试
runner = unittest.TextTestRunner()
runner.run(suite)