2023(春)Python程序设计作业6:基于unitest的Python测试

软工214罗杰 2023-春-学生 2023-06-09 21:03:25
  • 要求三角型的三个边长:A、B 和C(最大值不超过100)。当三边不可能构成三角形时提示错误。若是等腰三角形打印“等腰三角形”,若是等边三角形,则提示“等边三角形”,其它情形则返回“一般三角形”。  
  • 完成:Python程序代码、设计测试用例(覆盖有效等价类、无效等价类),并给出基于unittest测试的代码。

def triangle_type(a, b, c):
    max_side = max(a, b, c)
    if a + b + c <= 2 * max_side:
        return "Error: not a triangle"
    elif a == b == c:
        return "Equilateral triangle"
    elif a == b or a == c or b == c:
        return "Isosceles triangle"
    else:
        return "Scalene triangle"
 

import unittest
from triangle import triangle_type

class TestTriangle(unittest.TestCase):

    # 有效等价类测试用例
    def test_equilateral(self):
        self.assertEqual(triangle_type(5, 5, 5), "Equilateral triangle")

    def test_isosceles(self):
        self.assertEqual(triangle_type(5, 5, 6), "Isosceles triangle")
        self.assertEqual(triangle_type(5, 6, 5), "Isosceles triangle")
        self.assertEqual(triangle_type(6, 5, 5), "Isosceles triangle")

    def test_scalene(self):
        self.assertEqual(triangle_type(3, 4, 5), "Scalene triangle")
        self.assertEqual(triangle_type(5, 7, 9), "Scalene triangle")

    # 无效等价类测试用例
    def test_not_triangle(self):
        self.assertEqual(triangle_type(1, 2, 3), "Error: not a triangle")
        self.assertEqual(triangle_type(3, 1, 2), "Error: not a triangle")
        self.assertEqual(triangle_type(2, 3, 1), "Error: not a triangle")

if __name__ == '__main__':
    unittest.main()
 

结果:

 

...全文
24 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

163

社区成员

发帖
与我相关
我的任务
社区描述
软件工程老师
python 高校 江苏省·南通市
社区管理员
  • juking@ntu
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧