题目
附加要求:还要进行等价类划分测试,就是说要分别用边界值测试和等价类划分测试。
解决方法
编程语言:python
函数:
1)judge_triangle(lines, index):判断输入的三条边是否能够成三角形。参数解释;lines(包含三条边长度数据的数组);index:递归用的标志,表示当前正在处理第几条边。
2)kind_of_traingle(lines):当judge_triangle(lines, index)函数中判断三条边能顾构成一个三角形,就会调用此函数判断三角形的形状(等边三角形、等腰直角三角形、等腰三角形、直角三角形和普通三角形)。
输入方式:文件输入
代码实现
import re
def judge_triangle(lines, index):
# 判断边的情况
if lines[index - 1] > abs(lines[(index + 3) % 3] - lines[(index + 4) % 3]):
if lines[index - 1] < abs(lines[(index + 3) % 3] + lines[(index + 4) % 3]):
if index == 3:
return kind_of_traingle(lines)
else:
return judge_triangle(lines, index + 1)
return "非三角形"
def kind_of_traingle(lines):
if lines[0] == lines[1] == lines[2]:
return "等边三角形"
elif (lines[0] == lines[1]) or (lines[1] == lines[2] or lines[0] == lines[2]):
return "等腰三角形"
else:
return "一般三角形"
if __name__ == "__main__":
"""
边界值测试
"""
print("边界值测试".center(45, "="))
# 输入数据
all_inputs = []
# 预期输出
target = []
with open('data.txt', 'r', encoding="utf8") as lines:
for i in lines:
results = i.replace("\n", "").split(" ")
result = [eval(results[0]), eval(results[1]), eval(results[2])]
all_inputs.append(result)
target.append(results[3])
print("用例编号".center(6, " ") + "a".center(5, " ") + "b".center(5, " ") + "c".center(5, " ") + "预期结果".center(7, " ") + "实际结果".center(7, " ") + "结论".center(6, " "))
index = 0
for i in all_inputs:
index += 1
result = judge_triangle(i, 1)
if result == target[index-1]:
status = "pass"
else:
status = "fail"
print(str(index).center(9, " ") + str(i[0]).center(5, " ") + str(i[1]).center(5, " ") + str(i[2]).center(5, " ") + target[index-1].center(7, " ") + result.center(7, " ") + status.center(6, " "))
print("")
"""
等价类测试
"""
print("等价类测试".center(45, "="))
# 输入数据
all_inputs = []
# 预期输出
target = []
# 覆盖等价类
all_kind = []
with open('data-1.txt', 'r', encoding="utf8") as lines:
for i in lines:
results = i.replace("\n", "").split(",")
result = [results[0], results[1], results[2]]
all_inputs.append(result)
target.append(results[3])
all_kind.append(results[4])
print("用例编号".center(6, " ") + "a".center(5, " ") + "b".center(5, " ") + "c".center(5, " ") + "预期结果".center(7, " ") + "实际结果".center(7, " ") + "覆盖等价类".center(10, " ") + "结论".center(6, " "))
index = 0
for i in all_inputs:
index += 1
if (i[0] == " ") or (i[1] == " ") or (i[2] == " "):
result = "请输入数据"
elif re.match('[1-9]([0-9]*)?', i[0]) == None or re.match('[1-9]([0-9]*)?', i[1]) == None or re.match('[1-9]([0-9]*)?', i[2]) == None or ("." in i[0] or "." in i[1] or "." in i[2]):
result = "输入错误"
else:
result = judge_triangle([int(i[0]), int(i[1]), int(i[2])], 1)
if result == target[index-1]:
status = "pass"
else:
status = "fail"
print(str(index).center(9, " ") + str(i[0]).center(5, " ") + str(i[1]).center(5, " ") + str(i[2]).center(5, " ") + target[index-1].center(7, " ") + result.center(7, " ") + all_kind[index-1].center(10, " ") + status.center(6, " "))