此作业的要求参见:https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11243
结对伙伴:樊培毅
使用语言:phython
测试框架:unittest
编译环境:vs 2019
测试内容:四则运算试题生成
要求1 对每个功能,先给出测试用例,然后再编码功能。请注意把测试用例视为功能需求完成的检验指标。 (40分)
·对功能1测试
测试代码:
1import unittest 2 from f4 import * 3 4 class TestDict(unittest.TestCase): 5 6 def test_calc(self): 7 print('-----开始测试----- ') 8 str = ra_number() 9 print(str) 10 result = input('输入正确结果 >') 11 self.assertEqual(int(result), my_eval(str)) 12 print(' -----测试结束-----') 13 14 if __name__ == '__main__': 15 unittest.main() 16 c=input()
功能1测试截图
测试用例
·对功能2、3测试
测试代码:
1 # -*- coding: utf-8 -*- 2 import unittest 3 from f4 import * 4 import random 5 6 7 def create_equation(): # 随机生成算式 8 eq = [] 9 operator = ["+", "-", "*", "/"] 10 for i in range(3): 11 eq.append(random.randint(0, 10)) 12 eq.append(operator[random.randint(0, 3)]) 13 eq.append(random.randint(0, 10)) 14 p = random.randint(1, 5) 15 if p is 1: 16 eq.insert(0, "(") 17 eq.insert(4, ")") 18 elif p is 2: 19 eq.insert(0, "(") 20 eq.insert(6, ")") 21 elif p is 3: 22 eq.insert(2, "(") 23 eq.insert(6, ")") 24 elif p is 4: 25 eq.insert(2, "(") 26 eq.append(")") 27 elif p is 5: 28 eq.insert(4, "(") 29 eq.append(")") 30 return eq 31 32 33 def reverse_polish(equation): # 将算式转换为逆波兰表达式 34 result = [] 35 c = [] 36 slist = [i for i in equation] 37 cal = { 38 '+': 1, 39 '-': 1, 40 '*': 2, 41 '/': 2 42 } 43 cal1 = { 44 '(': 0, 45 ')': 0 46 } 47 for item in slist: 48 if item in range(0, 100): 49 result.append(item) 50 elif not c and item in cal.keys(): 51 c.append(item) 52 continue 53 elif c and item in cal.keys(): 54 for x in range(c.__len__()): 55 z = c[-1] 56 temp = cal[z] if z in cal else cal1[z] 57 if temp >= cal[item]: 58 result.append(c.pop()) 59 else: 60 c.append(item) 61 break 62 if not c: 63 c.append(item) 64 elif item is ")": 65 for x in range(c.__len__()): 66 if c[-1] == "(": 67 c.pop() 68 break 69 else: 70 result.append(c.pop()) 71 elif item is "(": 72 c.append(item) 73 # print(result,c) 74 for x in range(c.__len__()): 75 result.append(c.pop()) 76 return result 77 78 79 class PyStack(object): # 自定义栈 80 81 def __init__(self, initSize=20, incSize=10): 82 self.initSize = incSize 83 self.incSize = incSize 84 self.stackList = [] 85 self.top = self.bottom = 0 86 87 def push(self, ele): 88 if self.top - self.bottom >= self.initSize: 89 self.incSize += self.initSize 90 self.stackList.append(ele) 91 self.top += 1 92 93 def pop(self): 94 if self.top - self.bottom > 0: 95 self.top -= 1 96 ret = self.stackList.pop() 97 return ret 98 else: 99 return None 100 101 def len(self): 102 return self.top - self.bottom 103 104 105 def calculate(re_equation): # 计算逆波兰表达式 106 stack = PyStack() 107 sumEnd = 0 108 109 if len(re_equation) is 0: 110 return sumEnd 111 for i in re_equation: 112 if i in range(0, 100): 113 stack.push(float(i)) 114 elif '+' is i: 115 a = stack.pop() 116 b = stack.pop() 117 stack.push(b + a) 118 elif '-' is i: 119 a = stack.pop() 120 b = stack.pop() 121 stack.push(b - a) 122 elif '*' is i: 123 a = stack.pop() 124 b = stack.pop() 125 stack.push(b * a) 126 elif '/' is i: 127 a = stack.pop() 128 b = stack.pop() 129 if a == 0: 130 return False # print('%d/%d分子不能为0' % (b, a)) 131 else: 132 stack.push(b / a) 133 return stack.pop() 134 135 136 class F4Test(unittest.TestCase): 137 def test_f4(self): 138 pass 139 140 def test01_create_equation(self): # 测试顺序按函数名字字典顺序进行 141 print("create_equation函数单元测试开始:") 142 self.assertIsNotNone(create_equation()) 143 print("OK") 144 print("create_equation函数单元测试结束。 ") 145 146 def test02_reverse_polish(self): 147 eq = [] 148 print("reverse_polish函数单元测试开始:") 149 equation = input("输入一个四则运算(括号请使用英文版的括号):") 150 _eq_ans = input("输入正确的逆波兰表达式:") 151 list(equation) # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型 152 for temp in equation: 153 if '0' <= temp <= '9': 154 eq.append(int(temp)) 155 else: 156 eq.append(temp) 157 re_equation = reverse_polish(eq) 158 str_equation = "".join('%s' % id for id in re_equation) 159 self.assertEqual(_eq_ans, str_equation) 160 print("OK") 161 print("reverse_polish函数单元测试结束。 ") 162 163 164 165 def test03_calculate(self): 166 eq = [] 167 print("calculate函数单元测试开始:") 168 equation = input("输入一个可计算的逆波兰表达式:") 169 _eq_ans = input("输入该表达式的正确结果:") 170 list(equation) # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型 171 for temp in equation: 172 if '0' <= temp <= '9': 173 eq.append(int(temp)) 174 else: 175 eq.append(temp) 176 result = calculate(eq) 177 self.assertEqual(float(_eq_ans), result) 178 print("OK") 179 print("calculate函数单元测试结束。 ") 180 181 182 if __name__ == "__main__": 183 unittest.main()
测试截图:
测试用例: 要求2 在博客报告测试用例全部fail 到 全部pass 的过程,报告事实 (fail到修改代码或者测试用例,到pass) 以及收获。 除了最初的框架,测试用例中存在一次性pass没有经过fail的,也报告一次性通过,给出如此优秀地实现了这部分功能的代码。由2位同学中的一位发布博客提交到作业,指明自己的结对伙伴;另一位在作业中引用这一博客,指明自己的结对伙伴。(40分)
我们选择了unittest这个框架,由于之前很少编写测试代码,所以还是感觉很难的,于是参考了之前师哥师姐的测试代码,当然也不是直接能用,还是改了很久查了很多资料,尤其是对功能3的测试代码,花费很多时间也没能解决,请求了同学帮助,才完成此部分代码。我们两个人采用一个人编写测试用例、一个人测试的分工。
要求3 做好准备,在接下的一周你可能无法通过别人的测试用例。 (0分)
要求4 使用coding.net做版本控制。checkin 前要求清理 临时文件、可执行程序,通常执行 build-clean可以达到效果。(5分)
代码地址为https://hiii1.coding.net/p/sizeyunsuan/d/zhao/git/tree/master